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 Shnelvar 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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Ralph Shnelvar 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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

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

···

On Sun, Jan 17, 2010 at 5:29 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end

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

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

In Ruby 1.9:

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

You can also use __callee__.

Gary Wright

···

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

How do I get the name of a method?

def xxx
@WhatsMyName = ???
end

Gary Wright wrote:

···

On Jan 17, 2010, at 5:29 PM, Ralph Shnelvar 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 Wright

Cool! Didn't know about that.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

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 Shnelvar 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__.

Ralph Shnelvar wrote:

···

> On Jan 17, 2010, at 5:29 PM, Ralph Shnelvar 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__.

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

Justin Collins wrote:

Ralph Shnelvar 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 Ruby project is actually JRuby, so I don't quite have that
option.

-Justin

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.