Created
November 13, 2009 21:26
-
-
Save dwt/234180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def chain_method(with_symbol, &with_block_returned_from) | |
old_method = method with_symbol | |
new_method = with_block_returned_from.call old_method | |
eigenclass = class << self; self; end | |
eigenclass.class_eval do | |
define_method with_symbol, new_method | |
end | |
end | |
end | |
def foo(arg) | |
print 'in foo: ', arg, "\n" | |
end | |
puts | |
foo 'first call' | |
chain_method :foo do |old_method| | |
lambda do |arg| | |
puts 'in first block_eval' | |
old_method.call arg | |
end | |
end | |
puts | |
foo 'second call' | |
chain_method :foo do |old_method| | |
lambda do |arg| | |
puts 'in second block_eval' | |
old_method.call arg | |
end | |
end | |
puts | |
foo 'third call' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment