[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Getting the 'Sender' or 'Caller' object of a method

Peter Laurens

8/3/2007 1:20:00 PM

Hi,

I am not having much luck in finding out how to get the calling object
of a method.

Is there an implicit way that a method can get a pointer to the object
that called it, or do I have to write that explicitly, manually
including a 'sender' parameter for the method call myself?

I have looked at 'caller' but can only coax a string out of it.

Thanks in advance for your kind help!

- Nex
--
Posted via http://www.ruby-....

11 Answers

Robert Klemme

8/3/2007 1:25:00 PM

0

2007/8/3, Peter Laurens <peterlaurenspublic@gmail.com>:
> Hi,
>
> I am not having much luck in finding out how to get the calling object
> of a method.
>
> Is there an implicit way that a method can get a pointer to the object
> that called it, or do I have to write that explicitly, manually
> including a 'sender' parameter for the method call myself?
>
> I have looked at 'caller' but can only coax a string out of it.

Depends on what you want to do. If it's for debugging purposes you
can use set_trace_func to keep track of callers or just trace the
whole program execution.

If you need it for your program logic then you should pass the caller
- either as method parameter or set it as an attribute before the
call. Depends on what you do which is more appropriate.

Kind regards

robert

Robert Dober

8/3/2007 1:41:00 PM

0

On 8/3/07, Peter Laurens <peterlaurenspublic@gmail.com> wrote:
> Hi,
>
> I am not having much luck in finding out how to get the calling object
> of a method.
>
> Is there an implicit way that a method can get a pointer to the object
> that called it, or do I have to write that explicitly, manually
> including a 'sender' parameter for the method call myself?
>
> I have looked at 'caller' but can only coax a string out of it.
>
> Thanks in advance for your kind help!
>
> - Nex
> --
> Posted via http://www.ruby-....
>
>
Hmm maybe binding_of_caller might help, as far as I know Facet
implements it, and Why did so too, maybe just google it, I would not
know which one to recommend.

HTH
Robert

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Peter Laurens

8/3/2007 1:44:00 PM

0

Thanks to the both of you - it appears as though my best way to do this
is just manually then, via a passed 'sender' parameter.

Thanks!

- Nex
--
Posted via http://www.ruby-....

Wolfgang Nádasi-donner

8/3/2007 1:45:00 PM

0

Peter Laurens wrote:
> I am not having much luck in finding out how to get the calling object
> of a method.

I'm not sure to understand what you mean, because inside a method
"meth", which is used/called by an object "obj" by "obj.meth", the
object can be referenced by "self".

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....

ara.t.howard

8/3/2007 4:59:00 PM

0


On Aug 3, 2007, at 7:19 AM, Peter Laurens wrote:

> Hi,
>
> I am not having much luck in finding out how to get the calling object
> of a method.
>
> Is there an implicit way that a method can get a pointer to the object
> that called it, or do I have to write that explicitly, manually
> including a 'sender' parameter for the method call myself?
>
> I have looked at 'caller' but can only coax a string out of it.
>
> Thanks in advance for your kind help!
>
> - Nex
> --
> Posted via http://www.ruby-....
>

in many contexts this will work:

require 'binding_of_caller'

def method
caller = Binding_of_caller{|binding| eval 'self', binding}
end

notably NOT class methods though...

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Trans

8/3/2007 5:51:00 PM

0



On Aug 3, 9:59 am, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> On Aug 3, 2007, at 7:19 AM, Peter Laurens wrote:
>
>
>
> > Hi,
>
> > I am not having much luck in finding out how to get the calling object
> > of a method.
>
> > Is there an implicit way that a method can get a pointer to the object
> > that called it, or do I have to write that explicitly, manually
> > including a 'sender' parameter for the method call myself?
>
> > I have looked at 'caller' but can only coax a string out of it.
>
> > Thanks in advance for your kind help!
>
> > - Nex
> > --
> > Posted viahttp://www.ruby-....
>
> in many contexts this will work:
>
> require 'binding_of_caller'
>
> def method
> caller = Binding_of_caller{|binding| eval 'self', binding}
> end
>
> notably NOT class methods though...


You can always take a block and get the binding from it:

def method(&b)
callers_binding = b.send(:binding)
end

I works always. Yes, it means passing a block, but sometimes that's
useful anyway.

T.


Ben Bleything

8/3/2007 5:59:00 PM

0

On Fri, Aug 03, 2007, Robert Dober wrote:
> Hmm maybe binding_of_caller might help, as far as I know Facet
> implements it, and Why did so too, maybe just google it, I would not
> know which one to recommend.

IIRC, binding of caller hasn't worked since 1.8.4. It relied on a bug
that was fixed.

Ben

Ben Bleything

8/3/2007 6:02:00 PM

0

On Sat, Aug 04, 2007, Ben Bleything wrote:
> On Fri, Aug 03, 2007, Robert Dober wrote:
> > Hmm maybe binding_of_caller might help, as far as I know Facet
> > implements it, and Why did so too, maybe just google it, I would not
> > know which one to recommend.
>
> IIRC, binding of caller hasn't worked since 1.8.4. It relied on a bug
> that was fixed.

My mistake, I was thinking of Binding#of_caller, which was a different
thing, heh.

Ben

ara.t.howard

8/3/2007 7:35:00 PM

0


On Aug 3, 2007, at 11:50 AM, Trans wrote:

> You can always take a block and get the binding from it:
>
> def method(&b)
> callers_binding = b.send(:binding)
> end
>
> I works always. Yes, it means passing a block, but sometimes that's
> useful anyway.
>

??

cfp:~ > cat a.rb
def a &b
eval 'self', b.send(:binding)
end

require 'binding_of_caller'
def b
Binding.of_caller{|binding| eval 'self', binding}
end

p self
p a
p b


cfp:~ > ruby a.rb
main
nil
main


cfp:~ > ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]


am i misunderstanding?


a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Robert Dober

8/3/2007 7:47:00 PM

0

On 8/3/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Aug 3, 2007, at 11:50 AM, Trans wrote:
>
> > You can always take a block and get the binding from it:
> >
> > def method(&b)
> > callers_binding = b.send(:binding)
> > end
> >
> > I works always. Yes, it means passing a block, but sometimes that's
> > useful anyway.
> >
>
> ??
>
> cfp:~ > cat a.rb
> def a &b
> eval 'self', b.send(:binding)
> end
>
> require 'binding_of_caller'
> def b
> Binding.of_caller{|binding| eval 'self', binding}
> end
>
> p self
> p a
> p b
>
>
> cfp:~ > ruby a.rb
> main
> nil
> main
>
>
> cfp:~ > ruby -v
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
>
>
> am i misunderstanding?
yup, Tom asks us to kindly pass him a block

p a {}

one could consider it cheating, but it is incredibly useful, I did not
know about it.

Thx and Cheers
Robert
--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein