Is my method defined?

Discussion in 'Ruby' started by Fredrik, Sep 13, 2008.

  1. Fredrik

    Fredrik Guest

    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?
    Fredrik, Sep 13, 2008
    #1
    1. Advertising

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

    Stefano
    Stefano Crocco, Sep 13, 2008
    #2
    1. Advertising

  3. 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
    Sebastian Hungerecker, Sep 13, 2008
    #3
  4. Fredrik

    Fredrik Guest

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

    /Fredrik
    Fredrik, Sep 13, 2008
    #4
  5. 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.

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Sep 13, 2008
    #5
  6. Hi --

    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.


    Almost :)

    >> c.send("x")

    => nil
    >> c.x

    NoMethodError: private method `x' called for #<C:0x3c95ec>


    David

    --
    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!
    David A. Black, Sep 13, 2008
    #6
  7. Fredrik

    Fredrik Guest

    On Sep 13, 7:37 pm, 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.
    >
    > HTH,
    > Sebastian
    > --
    > Jabber:
    > ICQ: 205544826


    So how is send better than eval then?
    send("foo")
    eval("foo")
    Same, right?
    Fredrik, Sep 14, 2008
    #7
  8. Fredrik

    Fredrik Guest

    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


    Now I am utterly confused. Can anyone explain THIS behaviour :

    ### File : foo.rb ###
    def requiredfoo
    "I'm here!"
    end
    #####################

    irb> require 'foo.rb'
    irb> respond_to? 'requiredfoo'
    => false

    irb> def foo ; "I'm here!" ; end
    irb> respond_to? 'foo'
    => true

    That doesn't make sense at all, right?

    /Fredrik
    Fredrik, Sep 14, 2008
    #8
  9. Fredrik wrote:
    > So how is send better than eval then?


    It's more specific. And you can call the method on an object other than self
    without string manipulation.


    > send("foo")
    > eval("foo")
    > Same, right?


    Yes, but
    send("system('rm -rf /')") # Error
    eval("system('rm -rf /')") # Works

    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Sep 14, 2008
    #9
  10. Fredrik wrote:
    > irb> require 'foo.rb'
    > irb> respond_to? 'requiredfoo'
    > => false
    >
    > irb> def foo ; "I'm here!" ; end
    > irb> respond_to? 'foo'
    > => true


    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.

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Sep 14, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,737
    Keith Thompson
    Sep 27, 2005
  2. mike
    Replies:
    5
    Views:
    323
    Ben Pope
    Feb 22, 2006
  3. Angel Tsankov
    Replies:
    1
    Views:
    845
    Victor Bazarov
    Apr 5, 2006
  4. Replies:
    1
    Views:
    474
    Sion Arrowsmith
    Jul 10, 2008
  5. aidy
    Replies:
    5
    Views:
    216
    Marcin Mielżyński
    Jun 4, 2006
Loading...

Share This Page