How to - Get method's name?

H

Henry Savr

Dear Ruby gurus:

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

Thank you,
Henry
 
E

Eero Saynatkari

Henry said:
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.
 
H

Henry Savr

Eero said:
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,
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.
 
R

Rick DeNatale

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

ara.t.howard

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
 
R

Rick DeNatale

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/
 
M

Matthew Johnson

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: http://eigenclass.org/hiki.rb?breakpoint
+breaking+in+1.8.5.

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

Matthew
 
K

Kent Sibilev

Have a look at this article: http://eigenclass.org/hiki.rb?breakpoint
+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.
 
M

Matthew Johnson

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
 

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
474,210
Messages
2,571,091
Members
47,691
Latest member
Jenny-jane

Latest Threads

Top