1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Use anonymous method instead of method alias for Array, Class and Object extensions

This commit is contained in:
Mike Dvorkin 2011-05-13 13:58:00 -07:00
parent b9cf4a4e0a
commit 894e35ebd9
3 changed files with 11 additions and 7 deletions

View file

@ -14,9 +14,10 @@
#
class Array #:nodoc:
[ :-, :& ].each do |operator|
alias :"original_#{operator.object_id}" :"#{operator}"
original_operator = instance_method(operator)
define_method operator do |*args|
arr = self.__send__(:"original_#{operator.object_id}", *args)
arr = original_operator.bind(self).call(*args)
if self.instance_variable_defined?('@__awesome_methods__')
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
arr.sort! { |a, b| a.to_s <=> b.to_s } # Need the block since Ruby 1.8.x can't sort arrays of symbols.
@ -56,7 +57,10 @@ class Array #:nodoc:
arr = unless blk
original_grep(pattern)
else
original_grep(pattern) { |match| eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding); yield match }
original_grep(pattern) do |match|
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
yield match
end
end
if self.instance_variable_defined?('@__awesome_methods__')
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))

View file

@ -6,10 +6,10 @@
class Class #:nodoc:
# Remaining public/private etc. '_methods' are handled in core_ext/object.rb.
%w(instance_methods private_instance_methods protected_instance_methods public_instance_methods).each do |name|
alias :"original_#{name}" :"#{name}"
original_method = instance_method(name)
define_method name do |*args|
methods = self.__send__(:"original_#{name}", *args)
methods = original_method.bind(self).call(*args)
methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
methods.sort!
end

View file

@ -6,10 +6,10 @@
class Object #:nodoc:
# Remaining instance '_methods' are handled in core_ext/class.rb.
%w(methods private_methods protected_methods public_methods singleton_methods).each do |name|
alias :"original_#{name}" :"#{name}"
original_method = instance_method(name)
define_method name do |*args|
methods = self.__send__(:"original_#{name}", *args)
methods = original_method.bind(self).call(*args)
methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
methods.sort!
end