Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I know which method is called?

4 views
Skip to first unread message

Sam Kong

unread,
Jun 12, 2006, 9:19:01 PM6/12/06
to
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?

I think I can find which class or module newly define a method.
But I don't know whether a method is redefined by a class or module.

Thanks.

Sam

Trans

unread,
Jun 12, 2006, 10:41:04 PM6/12/06
to

Hi Sam,

You might look into Facets' nesting method. I don't recall exactly but
I think that might do the trick. Any way it shoul dgive you some leads
ast the very least.

http:://facets.rubyforge.org

T.

Robert Klemme

unread,
Jun 13, 2006, 7:36:37 AM6/13/06
to
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>

Kind regards

robert

Sam Kong

unread,
Jun 13, 2006, 11:44:06 AM6/13/06
to

Oh, now I see.
Thank you, Robert.

Sam

Sam Kong

unread,
Jun 13, 2006, 11:46:19 AM6/13/06
to

I'll check it out.
Thank you.

Sam

0 new messages