From: Rick DeNatale Date: 2006-08-26T02:18:58+09:00 Subject: Re: How to - Get method's name? On 8/24/06, ara.t.howard@noaa.gov wrote: > On Fri, 25 Aug 2006, Rick DeNatale wrote: > > Actually, I'd be more interested in finding out which object called > > me, or getting the equivalent of caller, but getting an array of the > > 'self's down the call stack. > > > > I haven't figured out how to do that yet in Ruby. > > google 'binding of caller' then > > eval 'self', Binding.of_caller Well, if I understand what I found, it's not what I'm looking for. Binding.of_caller seems to return the binding of the method which called it. So let's say I've got this: class String; def foo; who_called_me ;end;end class X; def test(obj); obj.foo; end; end; What I want is: x = X.new ==> # x.test("Hello") ==> # instead of x.test("Hello") ==> "Hello" which is what I get, if I replace who_called_me with eval 'self', binding.of_caller It doesn't seem possible to get what I'm looking for using the technique used in Binding.of_caller which is to set a trace to detect when we return to the caller (or is it from the caller). To do what I'm talking about seems to require examining the current call stack to get the receiver of the preceding message. It looks like the best you can do in ruby is to get a backtrace with Kernel#caller, or by raising and rescuing an exception, only gives strings describing where you were in the source, but with no access to the actual receiver objects. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ IPMS/USA Region 12 Coordinator http://ipmsr12.denhaven2.com/ Visit the Project Mercury Wiki Site http://www.mercuryspacecraft.com/