How to - Get method's name?

Dear Ruby gurus:

How to get the method’s name inside itself. For example to send it to
Error Processor

Thank you,
Henry

Henry S. wrote:

Dear Ruby gurus:

How to get the method’s name inside itself. For example to send it to
Error Processor

Thank you,
Henry

There is no good way of getting the method name, but you can see here
for solutions:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/205950

On the other hand, if you are doing it for error handling, why not just
use exceptions? They will have the stack trace with them.

-Justin

Justin C. wrote:

Henry S. wrote:

Dear Ruby gurus:

How to get the method’s name inside itself. For example to send it to
Error Processor

Thank you,
Henry

There is no good way of getting the method name, but you can see here
for solutions:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/205950

On the other hand, if you are doing it for error handling, why not just
use exceptions? They will have the stack trace with them.

-Justin
Thank you,
actually it is NOT for error processing, but the Error example, what I
expect to get, is clear for virtually everybody.
Henry

Eero S. wrote:

class Object
def method_name()
# Use Kernel.caller and some string manipulation here
end
end

class Foo
def foo
puts “My name is #{self.method_name}!”
end
end

I just do not see why this would be necessary.

Thank you,
Henry
Thank you,
sorry I had no chance to see it earlier
Regarding where it could be useful:

When you have to monitor app, which consists of thousands methods.

Monitor examples: process management, testing, security, etc…

In general, when somebody works for you, it is not a bad idea, to know
his/her name. It could be useful sometimes.

So I was wandering. Kernel uses and even sends a method name to app, and
I hoped, that it would share this knowledge with app by asking.
According you, it does not.
So, yes, thank you. I will have to cheat :)) and simulate errors until

Ruby developers will implement this in future editions.

I hope they read the forum.

On 8/17/06, Eero S. [email protected] wrote:

Henry S. wrote:

Dear Ruby gurus:

How to get the method’s name inside itself. For example to send it to
Error Processor

I just do not see why this would be necessary.

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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Henry S. wrote:

Dear Ruby gurus:

How to get the method’s name inside itself. For example to send it to
Error Processor

Can you describe a situation where you have access to
modify what a method does but not its name? I know it
IS possible but rather unusual.

Ruby’s exception handling will provide you with the
method name already. You could also do something like
this:

class Object
def method_name()
# Use Kernel.caller and some string manipulation here
end
end

class Foo
def foo
puts “My name is #{self.method_name}!”
end
end

I just do not see why this would be necessary.

Thank you,
Henry

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

-a

On 8/24/06, [email protected] [email protected] 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:0xb7d56034>

x.test(“Hello”) ==> #<X:0xb7d56034>

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/

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.

Have a look at this article: eigenclass.org
+breaking+in+1.8.5.

I believe the new library described here may be of use to you when it
is released.

Matthew

It seems that ruby-debug already provides almost all features planned
for this library:

I haven’t checked that library out but will have to do so. Thanks
for the pointer!

Matthew

On 8/25/06, Matthew J.son [email protected] wrote:

Have a look at this article: eigenclass.org
+breaking+in+1.8.5.

I believe the new library described here may be of use to you when it
is released.

It seems that ruby-debug already provides almost all features planned
for this library:

Consider this:

require “rubygems”
require ‘ruby-debug’
Debugger.start

module Kernel
def binding_n(n = 0)
frame = Debugger.current_context.frames[n+1]
frame.binding if frame
end
end

def test
puts eval(“var”, binding_n(1))
end

var = ‘Hello’
test

And I think that the overhead of both libraries should be the same,
which is substantial unfortunately.