Skip to content

Instantly share code, notes, and snippets.

@dwt
Created November 13, 2009 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dwt/234180 to your computer and use it in GitHub Desktop.
Save dwt/234180 to your computer and use it in GitHub Desktop.
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