Forum: Ruby Method name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
54a36519502d462c3622839ed1e51164?d=identicon&s=25 Michael (Guest)
on 2006-05-17 14:05
Hi,

I'm looking around, but I didn't find a replacement for c++
__PRETTY_FUNCTION__ or __FUNC__ macro.

How can I find out a method's name at runtime?

Best regards
Michael
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-05-17 14:36
(Received via mailing list)
On 5/17/06, Michael <Michael_ee@yahoo.com> wrote:
> Hi,
>
> I'm looking around, but I didn't find a replacement for c++
> __PRETTY_FUNCTION__ or __FUNC__ macro.
>
> How can I find out a method's name at runtime?

I assume you mean the name of the current method.  Here's one way.

name = caller(0).first.split.last

This gives it to you with single quotes around it which you may want
to strip off.
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-05-17 17:37
(Received via mailing list)
Mark Volkmann wrote:
> name = caller(0).first.split.last
>
> This gives it to you with single quotes around it which you may want
> to strip off.

Does this work?

  def __FUNCTION__
    caller(1).first[/ `(.*)'\Z/, 1]
  end

Cheers,
Dave
288733e11db1c6dbbf50871d2c6738c6?d=identicon&s=25 Uncutstone Wu (uncutstone)
on 2006-05-17 19:41
Dave Burt wrote:
> Mark Volkmann wrote:
>> name = caller(0).first.split.last
>>
>> This gives it to you with single quotes around it which you may want
>> to strip off.
>
> Does this work?
>
>   def __FUNCTION__
>     caller(1).first[/ `(.*)'\Z/, 1]
>   end
>
> Cheers,
> Dave

What if I am at the outmost level?
I cannot get a function name , just got something like this:
test.rb:2546

It may not be a perfect solution.
Ad4caa46eebe9347305177fc33468563?d=identicon&s=25 Mike Nelson (miken700)
on 2006-05-17 20:42
uncutstone wu wrote:
> Dave Burt wrote:
>> Mark Volkmann wrote:
>>> name = caller(0).first.split.last
>>>
>>> This gives it to you with single quotes around it which you may want
>>> to strip off.
>>
>> Does this work?
>>
>>   def __FUNCTION__
>>     caller(1).first[/ `(.*)'\Z/, 1]
>>   end
>>
>> Cheers,
>> Dave
>
> What if I am at the outmost level?
> I cannot get a function name , just got something like this:
> test.rb:2546
>
> It may not be a perfect solution.

It seems to work properly for me on the outermost level.

  def __FUNCTION__
    caller(1).first[/ `(.*)'\Z/, 1]
  end

  def test_method
    puts "i'm in '#{__FUNCTION__}'"
  end

  test_method
  puts "outer: i'm in '#{__FUNCTION__}'"

produces,

  i'm in 'test_method'
  outer: i'm in ''
288733e11db1c6dbbf50871d2c6738c6?d=identicon&s=25 Uncutstone Wu (uncutstone)
on 2006-05-17 21:00
Mike Nelson wrote:
>
>   i'm in 'test_method'
>   outer: i'm in ''


I mean at the outmost level, you are still execute in a function's
context, but you cannot get this function's name.
This topic is locked and can not be replied to.