Sha256: 04f366a56973ad8c4938f0b4131224a5b7ff8d4627c70f83dcae93a6d9a37087

Contents?: true

Size: 739 Bytes

Versions: 13

Compression:

Stored size: 739 Bytes

Contents

class Module

  private

  # Creates a new method for a pre-existing method.
  #
  # If _aka_ is given, then the method being redefined will
  # first be aliased to this name.
  #
  #   class Greeter
  #     def hello ; "Hello" ; end
  #   end
  #
  #   Greeter.new.hello   #=> "Hello"
  #
  #   class Greeter
  #     redefine_method( :hello, :hi ) do
  #       hi + ", friend!"
  #     end
  #   end
  #
  #   Greeter.new.hello   #=> "Hello, friend!"
  #
  # CREDIT: Trans

  def redefine_method(sym, aka=nil, &blk)
    raise ArgumentError, "method does not exist" unless method_defined?( sym )
    alias_method( aka, sym ) if aka
    undef_method( sym )
    define_method( sym, &blk )
  end

  alias_method :redef, :redefine_method

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
facets-2.9.0 lib/core/facets/module/redefine_method.rb
facets-2.9.0.pre.2 lib/core/facets/module/redefine_method.rb
facets-2.9.0.pre.1 lib/core/facets/module/redefine_method.rb
facets-2.8.4 lib/core/facets/module/redefine_method.rb
facets-2.8.3 lib/core/facets/module/redefine_method.rb
facets-2.8.2 lib/core/facets/module/redefine_method.rb
facets-2.8.1 lib/core/facets/module/redefine_method.rb
facets-2.8.0 lib/core/facets/module/redefine_method.rb
facets-2.7.0 lib/core/facets/module/redefine_method.rb
facets-2.6.0 lib/core/facets/module/redefine_method.rb
facets-2.5.0 lib/core/facets/module/redefine_method.rb
facets-2.5.1 lib/core/facets/module/redefine_method.rb
facets-2.5.2 lib/core/facets/module/redefine_method.rb