Sam Kong
6/13/2006 3:44:00 PM
Robert Klemme wrote:
> Sam Kong wrote:
> > Hello!
> >
> > When you send a message to an object, Ruby follows some path along
> > singleton object, class, included modules, superclasses, and so on.
> > Is there a way to know where the responding method is defined?
> >
> > For example:
> >
> > obj = MyClass.new
> > ...
> > obj.f
> >
> > The method f could be defined in MyClass or it could be just obj's
> > singleton method or whatever.
> > Is there a way to be sure of it?
>
> You can use #method for this:
>
> irb(main):001:0> class Foo; def foo()end; def bar() end end
> => nil
> irb(main):002:0> class Bar < Foo;def bar() end end
> => nil
> irb(main):003:0> Bar.new.method :foo
> => #<Method: Bar(Foo)#foo>
> irb(main):004:0> Bar.new.method :bar
> => #<Method: Bar#bar>
>
> singleton method:
>
> irb(main):005:0> obj = Bar.new
> => #<Bar:0x3c49d0>
> irb(main):006:0> def obj.foo() end
> => nil
> irb(main):007:0> obj.method :foo
> => #<Method: #<Bar:0x3c49d0>.foo>
Oh, now I see.
Thank you, Robert.
Sam