Passing method name to method?

Discussion in 'Ruby' started by Arfon Smith, Sep 28, 2007.

  1. Arfon Smith

    Arfon Smith Guest

    Hi, sorry if this isn't phrased quite as it should be!

    I want to have a generic 'find' method that can check to see if an
    object's attribute is true or false.

    Basically I want to pass the method name (param) to the list_by_param
    method but this doesn't seem to be working (I get an undefined local
    variable or method 'param' for main:Object) error.

    def list_by_param(param)
    puts "#{object.id}" if object.param == true
    end

    puts objects.list_by_param(param)


    Any idea where I'm going wrong?

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Arfon Smith, Sep 28, 2007
    #1
    1. Advertising

  2. On 9/28/07, Arfon Smith <> wrote:
    > Hi, sorry if this isn't phrased quite as it should be!
    >
    > I want to have a generic 'find' method that can check to see if an
    > object's attribute is true or false.
    >
    > Basically I want to pass the method name (param) to the list_by_param
    > method but this doesn't seem to be working (I get an undefined local
    > variable or method 'param' for main:Object) error.
    >
    > def list_by_param(param)
    > puts "#{object.id}" if object.param == true
    > end
    >
    > puts objects.list_by_param(param)
    >
    >

    I *think* what you are trying to do is

    def list_by_param(param)
    puts "#{object_id}" if object.send(param) == true
    end

    puts objects.list_by_param:)param)

    > Any idea where I'm going wrong?
    >
    > Thanks
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Logan Capaldo, Sep 28, 2007
    #2
    1. Advertising

  3. On 9/28/07, Arfon Smith <> wrote:
    > I want to have a generic 'find' method that can check to see if an
    > object's attribute is true or false.
    >
    > Basically I want to pass the method name (param) to the list_by_param
    > method but this doesn't seem to be working (I get an undefined local
    > variable or method 'param' for main:Object) error.



    class Object
    def objid_if_param(param)
    "#{self.__id__}" if self.__send__(param) == true
    end
    end

    class Foo
    attr_accessor :foo
    end

    bar = Foo.new
    baz = Foo.new
    baz.foo = true

    [ bar, baz ].each do |ob|
    id = ob.objid_if_param:)foo)
    puts id if id
    end

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
     
    Austin Ziegler, Sep 28, 2007
    #3
  4. Arfon Smith

    Arfon Smith Guest

    Logan Capaldo wrote:
    > On 9/28/07, Arfon Smith <> wrote:
    >> puts "#{object.id}" if object.param == true
    >> end
    >>
    >> puts objects.list_by_param(param)
    >>
    >>

    > I *think* what you are trying to do is
    >
    > def list_by_param(param)
    > puts "#{object_id}" if object.send(param) == true
    > end
    >
    > puts objects.list_by_param:)param)


    It works!

    Thanks. So I have to pass a symbol to the method to get this to work?
    --
    Posted via http://www.ruby-forum.com/.
     
    Arfon Smith, Sep 28, 2007
    #4
    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. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    834
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,826
    ding feng
    Jun 25, 2003
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,402
    Joe Smith
    Apr 22, 2007
  4. Gene Wirchenko

    Passing a Method Name to a Method

    Gene Wirchenko, Jun 22, 2011, in forum: Java
    Replies:
    33
    Views:
    1,057
    Fuschia, President-Elect of the Bright Purplish-Gr
    Jun 25, 2011
  5. Gene Wirchenko

    Passing a Method Name to a Method, Redux

    Gene Wirchenko, Jun 24, 2011, in forum: Java
    Replies:
    51
    Views:
    1,195
    Martin Gregorie
    Jul 24, 2011
Loading...

Share This Page