#!ruby | |
class Foo | |
def hello | |
puts "Hello" | |
end | |
end | |
def localize_method(klass, method, &block) | |
unbound = klass.instance_method(method) | |
klass.send(:define_method, method) do | |
block.call(unbound.bind(self)) | |
end | |
lambda { | |
klass.send(:define_method, method, unbound) | |
} | |
end | |
foo = Foo.new | |
foo.hello | |
#=> Hello | |
puts "====" | |
release = localize_method(Foo, :hello) do |orig| | |
puts "before hello" | |
orig.call | |
puts "after hello" | |
end | |
foo.hello | |
#=> before hello | |
# Hello | |
# after hello | |
release.call | |
puts "====" | |
foo.hello | |
#=> Hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment