Calling object method from variable

Discussion in 'Ruby' started by Frodo Larik, Aug 29, 2005.

  1. Frodo Larik

    Frodo Larik Guest

    Hi all,

    I have two questions regarding object methods

    1. Is it possible to call a method from a variable?
    Something like this:

    i_am_just_a_var = 'this_is_the_real_method'
    o = Object.new
    o.#{i_am_just_a_var} = 'some thing I want'



    2. What is the best way to check for a method of an object?

    After playing around in irb I came up with this:

    o = Object.new
    if ( o.methods.grep(/^some_method_name$/ )
    # do stuff
    else
    # do something else
    end


    Sincerely,

    Frodo Larik
    Frodo Larik, Aug 29, 2005
    #1
    1. Advertising

  2. 2005/8/29, Frodo Larik <>:
    > Hi all,
    >=20
    > I have two questions regarding object methods
    >=20
    > 1. Is it possible to call a method from a variable?
    > Something like this:
    >=20
    > i_am_just_a_var =3D 'this_is_the_real_method'
    > o =3D Object.new
    > o.#{i_am_just_a_var} =3D 'some thing I want'


    o.send("#{i_am_just_a_var}=3D", 'some thing I want')

    > 2. What is the best way to check for a method of an object?
    >=20
    > After playing around in irb I came up with this:
    >=20
    > o =3D Object.new
    > if ( o.methods.grep(/^some_method_name$/ )
    > # do stuff
    > else
    > # do something else
    > end


    o.respond_to? :foo
    o.send:)respond_to?, i_am_just_a_var)

    Cheers

    robert
    Robert Klemme, Aug 29, 2005
    #2
    1. Advertising

  3. Frodo Larik

    Lionel Thiry Guest

    Frodo Larik a écrit :
    > Hi all,
    >
    > I have two questions regarding object methods
    >
    > 1. Is it possible to call a method from a variable?
    > Something like this:
    >
    > i_am_just_a_var = 'this_is_the_real_method'
    > o = Object.new
    > o.#{i_am_just_a_var} = 'some thing I want'


    var_method = YourClass.instance_method:)real_method)
    o = YourClass.new
    var_method.bind(o).call

    or

    o = YourClass.new
    var_method = o.method:)real_method)
    var_method.call

    also

    o = YourClass.new
    var_method = o.method:)real_method=)
    var_method.call 'some thing I want'

    Please note: do not write var_method.call='some thing I want', you'll
    get an error.

    >
    >
    >
    > 2. What is the best way to check for a method of an object?
    >
    > After playing around in irb I came up with this:
    >
    > o = Object.new
    > if ( o.methods.grep(/^some_method_name$/ )
    > # do stuff
    > else
    > # do something else
    > end
    >
    >
    > Sincerely,
    >
    > Frodo Larik



    --
    Lionel Thiry

    Personal web site: http://users.skynet.be/lthiry/
    Lionel Thiry, Aug 29, 2005
    #3
    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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,879
    Eric Sosman
    Jan 7, 2005
  2. =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=
    Replies:
    2
    Views:
    372
    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=
    Mar 2, 2006
  3. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
  4. Elf M. Sternberg
    Replies:
    15
    Views:
    269
    Matthias Reitinger
    Jul 29, 2009
  5. why-em-jay

    Calling a method of the calling object ...

    why-em-jay, Sep 15, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    176
    Anno Siegel
    Sep 15, 2005
Loading...

Share This Page