Sha256: 44947da807df82c6dc89887e692d06b8d8b508761dbae57d7e524d04d039f4a2

Contents?: true

Size: 1.02 KB

Versions: 26

Compression:

Stored size: 1.02 KB

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!"
  #
  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

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCModule < Test::Unit::TestCase

    def fixture_method; "a"; end

    redefine_method( :fixture_method ) { nil }

    def test_redefine_method
      assert_equal( nil, fixture_method )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/module/redefine_method.rb
facets-0.9.0 lib/nano/module/redefine_method.rb
facets-1.0.3 packages/core/lib/facet/module/redefine_method.rb
facets-1.1.0 lib/facet/module/redefine_method.rb
facets-1.2.0 lib/facets/core/module/redefine_method.rb
facets-1.2.1 lib/facets/core/module/redefine_method.rb
facets-1.3.0 lib/facets/core/module/redefine_method.rb
facets-1.3.1 lib/facets/core/module/redefine_method.rb
facets-1.3.2 lib/facets/core/module/redefine_method.rb
facets-1.3.3 lib/facets/core/module/redefine_method.rb
facets-1.4.0 lib/facets/core/module/redefine_method.rb
facets-1.4.1 lib/facets/core/module/redefine_method.rb
facets-1.4.2 lib/facets/core/module/redefine_method.rb
facets-1.4.3 lib/facets/core/module/redefine_method.rb
facets-1.4.4 lib/facets/core/module/redefine_method.rb
facets-1.4.5 lib/facets/core/module/redefine_method.rb
facets-1.7.0 lib/facets/core/module/redefine_method.rb
facets-1.7.30 lib/facets/core/module/redefine_method.rb
facets-1.7.38 lib/facets/core/module/redefine_method.rb
facets-1.7.46 lib/facets/core/module/redefine_method.rb