diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index d769ac1..06358a6 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -8,7 +8,7 @@ # so do nothing for subsequent requires. # unless defined?(AwesomePrint::Inspector) - %w(array string method object class kernel).each do |file| + %w(awesome_method_array string method object class kernel).each do |file| require "awesome_print/core_ext/#{file}" end diff --git a/lib/awesome_print/core_ext/array.rb b/lib/awesome_print/core_ext/awesome_method_array.rb similarity index 75% rename from lib/awesome_print/core_ext/array.rb rename to lib/awesome_print/core_ext/awesome_method_array.rb index 54f7a68..b05dbf3 100644 --- a/lib/awesome_print/core_ext/array.rb +++ b/lib/awesome_print/core_ext/awesome_method_array.rb @@ -12,31 +12,24 @@ # # If you could think of a better way please let me know :-) # -class Array #:nodoc: +module AwesomeMethodArray #:nodoc: - alias :difference_without_awesome_print :- - def -(other_ary) - arr = difference_without_awesome_print(other_ary) - if self.instance_variable_defined?(:@__awesome_methods__) + def -(_other_ary) + super.tap do |arr| arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) end - arr end - alias :intersection_without_awesome_print :& - def &(other_ary) - arr = intersection_without_awesome_print(other_ary) - if self.instance_variable_defined?(:@__awesome_methods__) + def &(_other_ary) + super.tap do |arr| arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) end - arr end # # Intercepting Array#grep needs a special treatment since grep accepts # an optional block. # - alias :original_grep :grep def grep(pattern, &blk) # # The following looks rather insane and I've sent numerous hours trying @@ -62,9 +55,9 @@ class Array #:nodoc: # the comment :-) # arr = unless blk - original_grep(pattern) + super(pattern) else - original_grep(pattern) do |match| + super(pattern) do |match| # # The binding can only be used with Ruby-defined methods, therefore # we must rescue potential "ArgumentError: Can't create Binding from @@ -79,10 +72,8 @@ class Array #:nodoc: yield match end end - if self.instance_variable_defined?(:@__awesome_methods__) - arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) - arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap. - end + arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) + arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap. arr end end diff --git a/lib/awesome_print/core_ext/class.rb b/lib/awesome_print/core_ext/class.rb index f9b2743..7747b5a 100644 --- a/lib/awesome_print/core_ext/class.rb +++ b/lib/awesome_print/core_ext/class.rb @@ -16,6 +16,7 @@ class Class #:nodoc: define_method name do |*args| methods = original_method.bind(self).call(*args) methods.instance_variable_set(:@__awesome_methods__, self) + methods.extend(AwesomeMethodArray) methods end end diff --git a/lib/awesome_print/core_ext/object.rb b/lib/awesome_print/core_ext/object.rb index bdc1655..f3685bf 100644 --- a/lib/awesome_print/core_ext/object.rb +++ b/lib/awesome_print/core_ext/object.rb @@ -16,6 +16,7 @@ class Object #:nodoc: define_method name do |*args| methods = original_method.bind(self).call(*args) methods.instance_variable_set(:@__awesome_methods__, self) + methods.extend(AwesomeMethodArray) methods end end