偶然搜到DHH给Matz的一封信,关于undef_method和remove_method

Hi,

In message "[ruby-talk:01169] undef_method vs. remove_method"
    on 00/01/23, Dave Thomas <Dave / thomases.com> writes:

|OK - I guess I need to understand the source a bit better. What's the
|difference between Module.undef_method and Module.remove_method?

`undef_method' makes a method undefined, `remove_method' removes it.
Ok, here's the example,

  class Foo
    def foo
      print "foo at Foo\n"
    end
    def bar
      print "bar at Foo\n"
    end
  end
  class Bar<Foo
    def foo
      print "foo at Bar\n"
    end
    def bar
      print "bar at Bar\n"
    end

    remove_method :foo  # removes `foo at Bar'
    undef_method :bar   # undefine `bar' for class Bar

  end

  bar = Bar.new
  bar.foo       # calls foo at Foo
  bar.bar       # error; whole `bar' made undefined

你可能感兴趣的:(Ruby)