name of method?

R

Ralph Shnelvar

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end


What should ??? be? I want it to return "xxx".
 
M

Marnen Laibow-Koser

Ralph said:
How do I get the name of a method?

def xxx
@WhatsMyName = ???
end


What should ??? be? I want it to return "xxx".

Then ??? should be "xxx". No shortcut here, I think.

Also, @WhatsMyName is a poor variable name. You want @whats_my_name or
(better) @name.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
M

Marnen Laibow-Koser

Ralph said:
How do I get the name of a method?

def xxx
@WhatsMyName = ???
end


What should ??? be? I want it to return "xxx".

I should mention that Method#name would work if you could get a hold of
the Method object, but I don't see how you'd do that from within the
method definition.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
R

Rick DeNatale

How do I get the name of a method?

def xxx
=A0@WhatsMyName =3D ???
end


What should ??? be? =A0I want it to return "xxx".

Here's a bit of a hack using caller.

def method_name
caller.first.match(/in `(.*)'/)[1]
end

class Foo
def bar
puts method_name
end
alias_method :baz, :bar
end

Foo.new.bar
Foo.new.baz

outputs:
bar
baz


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
G

Gary Wright

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end

In Ruby 1.9:

def foo
puts "this is method: #{__method__}"
end

You can also use __callee__.

Gary Wright
 
R

Ralph Shnelvar

GW> In Ruby 1.9:

GW> def foo
GW> puts "this is method: #{__method__}"
GW> end

GW> You can also use __callee__.

Ok ... The Pickaxe book warns about not going to 1.9 because of library issues.

Is it safe to upgrade to 1.9 now? I see that the one-click installer is still a release candidate.

Is there a more-or-less central place that discusses 1.9 vis-a-vis compatibility with all the gems and libraries and drop-ins that one might be using?
 
J

Justin Collins

Ralph said:
GW> On Jan 17, 2010, at 5:29 PM, Ralph Shnelvar wrote:



GW> In Ruby 1.9:

GW> def foo
GW> puts "this is method: #{__method__}"
GW> end

GW> You can also use __callee__.

Ok ... The Pickaxe book warns about not going to 1.9 because of library issues.

Is it safe to upgrade to 1.9 now? I see that the one-click installer is still a release candidate.

Is there a more-or-less central place that discusses 1.9 vis-a-vis compatibility with all the gems and libraries and drop-ins that one might be using?

I would say to use 1.9 unless it does not work for you.

You can check some compatibility information at http://isitruby19.com/

-Justin
 

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

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,423
Latest member
henerygril

Latest Threads

Top