Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > using the current method name within current method

Reply
Thread Tools

using the current method name within current method

 
 
Matthew Heidemann
Guest
Posts: n/a
 
      07-31-2006
Is there a way to get the current method name within the current method?

def test_method
puts self.current_method
end

--- output ----
test_method

Thanks,
Matt

 
Reply With Quote
 
 
 
 
Daniel Harple
Guest
Posts: n/a
 
      07-31-2006
On Jul 30, 2006, at 11:37 PM, Matthew Heidemann wrote:

> Is there a way to get the current method name within the current
> method?
>
> def test_method
> puts self.current_method
> end
>
> --- output ----
> test_method


In 1.9 (Ruby CVS HEAD) there is #__method__ and #__callee__: http://
eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90

-- Daniel


 
Reply With Quote
 
 
 
 
Aleks Kissinger
Guest
Posts: n/a
 
      07-31-2006
This is kindof a hack, but it seems to work:

def get_mname
caller[0]=~/`(.*?)'/ # note the first quote is a backtick
$1
end

def dinosaur
puts get_mname
end

dinosaur()
=> 'dinosaur'


On 7/30/06, Daniel Harple <(E-Mail Removed)> wrote:
> On Jul 30, 2006, at 11:37 PM, Matthew Heidemann wrote:
>
> > Is there a way to get the current method name within the current
> > method?
> >
> > def test_method
> > puts self.current_method
> > end
> >
> > --- output ----
> > test_method

>
> In 1.9 (Ruby CVS HEAD) there is #__method__ and #__callee__: http://
> eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90
>
> -- Daniel
>
>
>


 
Reply With Quote
 
Matt Todd
Guest
Posts: n/a
 
      07-31-2006
If you wanted to modularize it just for the sake of it, you could do
something similar to the following:

module CurrentMethodName
def this_method
caller[0]=~/`(.*?)'/
$1
end
end

And, then, use it like this:

class Foo
include CurrentMethodName
def bar
this_method
end
end

Hope this cleans it up a bit for you. It's a holdover until 1.9.

M.T.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-31-2006
Matt Todd wrote:
> If you wanted to modularize it just for the sake of it, you could do
> something similar to the following:
>
> module CurrentMethodName
> def this_method
> caller[0]=~/`(.*?)'/
> $1
> end
> end


You can rewrite that as

def this_method
caller[0][/`([^']*)'/, 1]
end

and thusly avoid the global variable.

Kind regards

robert
 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      07-31-2006

On Jul 31, 2006, at 4:25 AM, Robert Klemme wrote:

> Matt Todd wrote:
>> If you wanted to modularize it just for the sake of it, you could do
>> something similar to the following:
>> module CurrentMethodName
>> def this_method
>> caller[0]=~/`(.*?)'/
>> $1
>> end
>> end

>
> You can rewrite that as
>
> def this_method
> caller[0][/`([^']*)'/, 1]
> end
>
> and thusly avoid the global variable.
>
> Kind regards
>
> robert
>


I have a sneaking suspicion he might have done it with the global
variable (although $1 isn't really global) on purpose, since =~ /
literal/ is the fastest way to do a regexp.


 
Reply With Quote
 
Matt Todd
Guest
Posts: n/a
 
      08-01-2006
As much as I'd like to take credit for knowing its superior speed, I
can't as I stole it from Aleks.

However, I did know that $1 wasn't a true global in the sense of the
term, but a magic variable specific to the regexp test. It's commonly
acceptable, and, hey, if it's faster, why not, right?

A side note: this is one of the first things I've worked with that I
decided to make into something that was includable.... It's a
milestone in my way of thinking and coding with Ruby! Yay! (Sorry, had
to tell someone!)

Cheers!

M.T.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-01-2006
Logan Capaldo wrote:
>
> On Jul 31, 2006, at 4:25 AM, Robert Klemme wrote:
>
>> Matt Todd wrote:
>>> If you wanted to modularize it just for the sake of it, you could do
>>> something similar to the following:
>>> module CurrentMethodName
>>> def this_method
>>> caller[0]=~/`(.*?)'/
>>> $1
>>> end
>>> end

>>
>> You can rewrite that as
>>
>> def this_method
>> caller[0][/`([^']*)'/, 1]
>> end
>>
>> and thusly avoid the global variable.
>>
>> Kind regards
>>
>> robert
>>

>
> I have a sneaking suspicion he might have done it with the global
> variable (although $1 isn't really global) on purpose, since =~
> /literal/ is the fastest way to do a regexp.


I did a benchmark just to put some meat to this:

13:37:39 [Temp]: ./bm.rb
Rehearsal --------------------------------------------
global 1.859000 0.000000 1.859000 ( 1.863000)
String[] 2.156000 0.000000 2.156000 ( 2.180000)
----------------------------------- total: 4.015000sec

user system total real
global 1.828000 0.000000 1.828000 ( 1.872000)
String[] 2.094000 0.000000 2.094000 ( 2.172000)

IOW, the global var version uses 87,3% of the time or is 15% faster.

Kind regards

robert


#!/usr/bin/env ruby

require 'benchmark'

REPEAT = 1_00_000

def this_method_1
caller[0][/`([^']*)'/, 1]
end

def this_method_2
caller[0] =~ /`([^']*)'/ and $1
end

Benchmark.bmbm do |bm|
bm.report "global" do
REPEAT.times do
this_method_2
end
end

bm.report "String[]" do
REPEAT.times do
this_method_1
end
end
end

 
Reply With Quote
 
Aleks Kissinger
Guest
Posts: n/a
 
      08-02-2006
Depending on how you feel about mucking with core ruby types, a method
like this one seems like a decent candidate for mixing in to Object.

class Object
def this_method
caller[0]=~/`(.*?)'/
$1
end
end

Course I'm one of those crazy irb hack loving people that mixes
convenience methods into everything....

> >> Matt Todd wrote:
> >>> If you wanted to modularize it just for the sake of it, you could do
> >>> something similar to the following:
> >>> module CurrentMethodName
> >>> def this_method
> >>> caller[0]=~/`(.*?)'/
> >>> $1
> >>> end
> >>> end


<snip>

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-03-2006
Aleks Kissinger wrote:
> Depending on how you feel about mucking with core ruby types, a method
> like this one seems like a decent candidate for mixing in to Object.
>
> class Object
> def this_method
> caller[0]=~/`(.*?)'/
> $1
> end
> end


These methods are typically put into Kernel and made private. Also, you
don't check whether the RX actually matches. I'd also prefer to change
the RX to a more efficient one. So that would give

module Kernel
private
def this_method
caller[0] =~ /`([^']*)'/ and $1
end
end

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I programmatically find the name of a method from within that method? kj7ny Python 9 08-09-2007 06:50 AM
Getting method name from within the class method Mitko Haralanov Python 17 10-20-2006 02:11 AM
Reflection to get method name from within method? Ezra Zygmuntowicz Ruby 2 07-10-2006 09:47 PM
HttpContext.Current.User.Identity.Name AND Context.User.Identity.Name; nalbayo ASP .Net 2 11-11-2005 11:12 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments