Hello!
I want do do something like that:
def foo
puts the_name_the_method_was_called_with
end ; alias bar foo
foo => "foo"
bar => “bar”
Is there a thing like the_name_the_method_was_called_with?
Thanks in advance,
-kfk
···
–
UNIX has grown fat.
-Rob Pike, 1983
Hi,
I think this has been asked in the past; for example, see the thread
starting at http://www.ruby-talk.org/38976. One of the answers (by
Matz) is to extract it from ‘caller(0)’.
Regards,
Bill
···
Klaus Fabritius kfk@wasserhase.de wrote:
Hello!
I want do do something like that:
def foo
puts the_name_the_method_was_called_with
end ; alias bar foo
foo => “foo”
bar => “bar”
Is there a thing like the_name_the_method_was_called_with?
caller(0) may help.
% ri caller
[…]
Returns the current execution stack—an array containing strings
in the form file:line'' or
file:line: in method'''. The optional anInteger parameter determines the number of initial stack entries to omit from the result. [...] c(0) #=> ["prog:2:in
a’", “prog:5:in b'", "prog:8:in
c’”,
“prog:10”]
c(1) #=> [“prog:5:in b'", "prog:8:in
c’”, “prog:11”]
c(2) #=> [“prog:8:in `c’”, “prog:12”]
c(3) #=> [“prog:13”]
I use caller(0) in one of the functions that I’ve translated, and I
have to say that I really don’t like it for a few reasons. I’ve only
just identified the core reason that I don’t like it. It returns an
Array of Strings, which then have to be parsed in one of two
optional forms.
Shouldn’t it return an Array of Calls, where a Call contains the
same information as is in the string and, in fact, has a #to_s that
outputs in the same form as what we see above? Right now, I can’t
interact with the results of Caller in an “intuitively” OO fashion,
like I can (just about) with everything else in Ruby.
I just quickly created a simulation of it with the attached
caller.rb file, but I think that this should probably be the way
that Caller works. I’m sure there are other things that could/should
be added, but I shouldn’t have to “parse” the string manually every
time I want to deal with caller. Most of the time, I just want to
know the name of the method that called the current method.
Note that for the replacement to caller that I’m incrementing the
caller parameter by one; this is so that the fact that there’s one
more level of indirection present is hidden.
-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.26 at 19.07.57
caller.rb (751 Bytes)
···
On Wed, 27 Nov 2002 08:54:07 +0900, Yukihiro Matsumoto wrote:
Thanks to you and Bill.
I hoped that there is something like local_variables. Well, now it is
the first method in my own utils-package:
def method_name
/`(.+)'$/.match(caller(1)[0])[1]
end
Hm, it should be the last method in the file because it kills syntax
highlighting
-kfk
···
Yukihiro Matsumoto matz@ruby-lang.org wrote:
on 02/11/27, Klaus Fabritius kfk@wasserhase.de writes:
Is there a thing like the_name_the_method_was_called_with?
caller(0) may help.
–
UNIX has grown fat.
-Rob Pike, 1983
Austin Ziegler austin@halostatue.ca writes:
Shouldn’t it return an Array of Calls, where a Call contains the
same information as is in the string and, in fact, has a #to_s that
You could get the Wayne’s stack_frames patch:
http://yagni.com/stack_frames/index.php
YS.
Hi –
Is there a thing like the_name_the_method_was_called_with?
caller(0) may help.
Thanks to you and Bill.
I hoped that there is something like local_variables. Well, now it is
the first method in my own utils-package:
def method_name
/`(.+)'$/.match(caller(1)[0])[1]
end
I know it’s a longshot, but:
dblack@laptop:~/hacking$ cat some`file
def method_name
/`(.+)'$/.match(caller(1)[0])[1]
end
def x
puts method_name
end
x
dblack@laptop:~/hacking$ ruby some`file
file:6:in `x
dblack@laptop:~/hacking$
I honestly don’t know what the perfect tweak would be.
David
···
On Thu, 28 Nov 2002, Klaus Fabritius wrote:
Yukihiro Matsumoto matz@ruby-lang.org wrote:
on 02/11/27, Klaus Fabritius kfk@wasserhase.de writes:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
Shouldn’t there be a class to encapsulate what is returned by each array
entry in from
caller so that we don’t have to take it apart with a regex?
For example caller could return an array of ProgramCounter objects where
ProgramCounter = Struct.new(“ProgramCounter”, :filename, :linenum,
:method_name)
that way the code below would be
def method_name
caller((1)[0]).method_name
end
Steve Tuckner
···
-----Original Message-----
From: Klaus Fabritius [mailto:kfk@wasserhase.de]
Sent: Wednesday, November 27, 2002 10:47 AM
To: ruby-talk ML
Subject: Re: The names of a method
Yukihiro Matsumoto matz@ruby-lang.org wrote:
on 02/11/27, Klaus Fabritius kfk@wasserhase.de writes:
Is there a thing like the_name_the_method_was_called_with?
caller(0) may help.
Thanks to you and Bill.
I hoped that there is something like local_variables. Well, now it is
the first method in my own utils-package:
def method_name
/`(.+)'$/.match(caller(1)[0])[1]
end
Hm, it should be the last method in the file because it kills syntax
highlighting
-kfk
–
UNIX has grown fat.
-Rob Pike, 1983
Austin Ziegler austin@halostatue.ca writes:
Shouldn’t it return an Array of Calls, where a Call contains the
same information as is in the string and, in fact, has a #to_s
that
You could get the Wayne’s stack_frames patch:
http://yagni.com/stack_frames/index.php
I don’t particularly care for that because I don’t compile my own
version of Ruby (except on Linux, which is a fraction of my total
development time). I’d rather use something which is “standard
Ruby”. IMO, the stack_frames patch should probably become part of
standard Ruby – although the only time that I’ve needed caller(0)
so far, I just care about the immediate caller (to determine whether
the function was being called by another function in the same class;
it has a minor effect on behaviour).
-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.26 at 22.39.08
···
On Wed, 27 Nov 2002 11:46:02 +0900, Yohanes Santoso wrote: