Is my method defined?

I have a method by the name methodA. I want to access this method like
this

a = 'methodA'
eval(a)

But how do I know if the variable a actually holds the name of a
defined method? An exception is raised if I try to run eval(a) with an
incorrect method name, but I need to know this before I call eval(a).
How do I do that?

I found this solution:

def method?(arg)
begin
method(a)
rescue
nil
end
end

which does work, but why is this function "method?" not already in the
Ruby language then?

Advertisements

Alle Saturday 13 September 2008, Fredrik ha scritto:
> I have a method by the name methodA. I want to access this method like
> this
>
> a = 'methodA'
> eval(a)
>
> But how do I know if the variable a actually holds the name of a
> defined method? An exception is raised if I try to run eval(a) with an
> incorrect method name, but I need to know this before I call eval(a).
> How do I do that?
>
> I found this solution:
>
> def method?(arg)
> begin
> method(a)
> rescue
> nil
> end
> end
>
> which does work, but why is this function "method?" not already in the
> Ruby language then?

In my opinion, for two reasons:
1) the name is misleading. Methods ending in ? usually are method which only
answer a Yes/No question, without taking any action. The method you propose,
instead, perform an action and doesn't give an answer to a question. If I were
a user seeing a method called 'method?', I'd think it's a synonym for
respond_to?, or similar to it.
2) This functionality isn't needed very often, and it's very easy to write
your own method if you need it, as you have done.

On Sep 13, 6:24 pm, Sebastian Hungerecker <>
wrote:
> Fredrik wrote:
> > I have a method by the name methodA. I want to access this method like
> > this
>
> > a = 'methodA'
> > eval(a)
>
> You should use send if a only contains a methodname.
>
> > But how do I know if the variable a actually holds the name of a
> > defined method?
>
> respond_to? method_name
>
> HTH,
> Sebastian
> --
> NP: Metallica - The Day That Never Comes
> Jabber:
> ICQ: 205544826

I got it! respond_to? is what I was looking for. Thanks!
I will open my Ruby book and look into that send thing...

On Sat, 13 Sep 2008, Sebastian Hungerecker wrote:
> Fredrik wrote:
>> I will open my Ruby book and look into that send thing...
>
> send is quite simple actually:
> send("foo") is the same as foo
> send("foo", bar) is the same as foo(bar)
> object.send("foo", bar) is the same as object.foo(bar)
> That's basically it.

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!

On Sep 13, 6:24 pm, Sebastian Hungerecker <>
wrote:
> Fredrik wrote:
> > I have a method by the name methodA. I want to access this method like
> > this
>
> > a = 'methodA'
> > eval(a)
>
> You should use send if a only contains a methodname.
>
> > But how do I know if the variable a actually holds the name of a
> > defined method?
>
> respond_to? method_name
>
> HTH,
> Sebastian
> --
> NP: Metallica - The Day That Never Comes
> Jabber:
> ICQ: 205544826

respond_to? returns false for private methods. If you define a method outside
of a class/module, it's a private instance method of Object by default, except
when you define it in irb in which case it will be public for whatever reason.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!