How can I know which method is called?

S

Sam Kong

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
 
T

Trans

Sam said:
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.

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.
 
R

Robert Klemme

Sam said:
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
 
S

Sam Kong

Robert said:
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
 
S

Sam Kong

Trans said:
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

I'll check it out.
Thank you.

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top