Name of method?

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end

What should ??? be? I want it to return “xxx”.

Ralph S. wrote:

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
removed[email protected]

Ralph S. wrote:

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
[email protected]

On Sun, Jan 17, 2010 at 5:29 PM, Ralph S. [email protected]
wrote:

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end

What should ??? be? I 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


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

Gary W. wrote:

On Jan 17, 2010, at 5:29 PM, Ralph S. wrote:

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 W.

Cool! Didn’t know about that.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

GW> On Jan 17, 2010, at 5:29 PM, Ralph S. wrote:

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end

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?

On Jan 17, 2010, at 5:29 PM, Ralph S. wrote:

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 W.

Ralph S. wrote:

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

Justin C. wrote:

Ralph S. wrote:

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/

I would not use 1.9 with Rails yet, but some people do. My one
non-Rails R. project is actually JRuby, so I don’t quite have that
option.

-Justin

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs