[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How can I know which method is called?

Sam Kong

6/13/2006 1:19:00 AM

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

4 Answers

Trans

6/13/2006 2:41:00 AM

0


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

Robert Klemme

6/13/2006 11:37:00 AM

0

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

6/13/2006 3:44:00 PM

0


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

Sam Kong

6/13/2006 3:46:00 PM

0


Trans 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?
> >
> > 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

I'll check it out.
Thank you.

Sam