[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to - Get method's name?

Henry Savr

8/17/2006 2:17:00 PM

Dear Ruby gurus:

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

Thank you,
Henry

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

10 Answers

Justin Collins

8/17/2006 5:15:00 PM

0

Henry Savr 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-t...

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

Henry Savr

8/17/2006 5:25:00 PM

0

Justin Collins wrote:
> Henry Savr 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-t...
>
> 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


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

e

8/17/2006 5:59:00 PM

0

Henry Savr 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


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

Henry Savr

8/24/2006 4:44:00 PM

0

Eero Saynatkari 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.

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

Rick DeNatale

8/24/2006 10:36:00 PM

0

On 8/17/06, Eero Saynatkari <eero.saynatkari@kolumbus.fi> wrote:
> Henry Savr 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.denh...

Ara.T.Howard

8/24/2006 11:13:00 PM

0

Rick DeNatale

8/25/2006 5:19:00 PM

0

On 8/24/06, ara.t.howard@noaa.gov <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: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.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Matthew Johnson

8/25/2006 5:38:00 PM

0

> 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?...
+breaking+in+1.8.5.

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

Matthew

Kent Sibilev

8/25/2006 6:09:00 PM

0

On 8/25/06, Matthew Johnson <musical.matthew@mac.com> wrote:
>
> Have a look at this article: http://eigenclass.org/hiki.rb?...
> +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.

--
Kent
---
http://www.dat...

Matthew Johnson

8/25/2006 6:22:00 PM

0

> 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