Sha256: 3a9fbbb2f761b971d46d15843e80ee4cc9fd8608579e0cba755e0ba9ca8b690b

Contents?: true

Size: 693 Bytes

Versions: 1

Compression:

Stored size: 693 Bytes

Contents

class Module
  # Creates a new method wrapping the previous of 
  # the same name, passing it into the  block 
  # definition of the new method.
  #
  # This can not be used to wrap methods that take
  # a block.
  #
  #   require 'facet/module/wrap_method'
  #
  #   wrap_method( sym ) { |old_meth, *args| 
  #     old_meth.call 
  #     ...
  #   }
  #  
  def wrap_method( sym, &blk )
    raise ArgumentError, "method does not exist" unless method_defined?( sym )
    old = instance_method(sym)
    undef_method(sym);
    define_method(sym) { |*args| blk.call(old.bind(self), *args) }
  end
  
  # Private alias for wrap_method.
  alias_method( :wrap, :wrap_method )
  private :wrap
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/module/wrap_method.rb