Method: Module#wrap_method
- Defined in:
- lib/core/facets/module/wrap_method.rb
#wrap_method(sym, &blk) ⇒ Object Also known as: wrap
Creates a new method wrapping the previous of the same name. Reference to the old method is passed into the new definition block as the first parameter.
class WrapExample
def foo
"foo"
end
wrap_method(:foo) do |old_meth, *args|
old_meth.call + '!'
end
end
example = WrapExample.new
example.foo #=> 'foo!'
Keep in mind that this cannot be used to wrap methods that take a block.
CREDIT: Trans
27 28 29 30 |
# File 'lib/core/facets/module/wrap_method.rb', line 27 def wrap_method( sym, &blk ) old = instance_method(sym) define_method(sym) { |*args| blk.call(old.bind(self), *args) } end |