using send to define object methods

Discussion in 'Ruby' started by David Miller, Sep 18, 2010.

  1. David Miller

    David Miller Guest

    Hello

    If I want to add a method to a class, I can use send as shown below. Is
    there a similar way to use send to add a method to an object?

    class MyClass
    end

    MyClass.send:)define_method, :poof) {'I say poof!'}

    x = MyClass.new
    x.poof # => "I say poof!"


    I can define an object method as follows:

    def x.object_poof
    'I say object_poof'
    end

    x.object_poof # => "I say object_poof"

    I would like to use send to define object methods but I have not figured
    out how.

    Thanks
    Dave Miller
    --
    Posted via http://www.ruby-forum.com/.
    David Miller, Sep 18, 2010
    #1
    1. Advertising

  2. David Miller

    Y. NOBUOKA Guest

    Hello,

    If you execute your script on Ruby 1.9, you can use
    Object#define_singleton_method to add a method to an object.

    # --- begin: example ---

    class MyClass
    end
    MyClass.send:)define_method, :poof) {'I say poof!'}
    x =3D MyClass.new
    y =3D MyClass.new

    x.define_singleton_method( :poof_singleton ) { "Here is in a singleton
    method." }
    x.poof_singleton #=3D> "Here is in a singleton method."
    y.poof_singleton #=3D> NoMethodError

    # --- end: example ---

    The method Object#define_singleton_method is able to be used on Ruby
    1.9, but I couldn't find out its doc...


    2010/9/18 David Miller <>:
    > Hello
    >
    > If I want to add a method to a class, I can use send as shown below. =A0I=

    s
    > there a similar way to use send to add a method to an object?
    >
    > class MyClass
    > end
    >
    > MyClass.send:)define_method, :poof) {'I say poof!'}
    >
    > x =3D MyClass.new
    > x.poof # =3D> "I say poof!"
    >
    >
    > I can define an object method as follows:
    >
    > def x.object_poof
    > =A0'I say object_poof'
    > end
    >
    > x.object_poof # =3D> "I say object_poof"
    >
    > I would like to use send to define object methods but I have not figured
    > out how.
    >
    > Thanks
    > Dave Miller
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    --=20
    NOBUOKA Yuya
    e-mail:
    Y. NOBUOKA, Sep 18, 2010
    #2
    1. Advertising

  3. David Miller wrote:
    > I would like to use send to define object methods but I have not figured
    > out how.


    There is a magic incantation to get at the singleton class:

    object_poof = Object.new
    class <<object_poof; self; end.send:)define_method, :poof) {'I say
    poof!'}
    object_poof.poof

    I would personally use .class_eval { define_method .. } instead of
    send:)define_method ..) but that's just preference.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 18, 2010
    #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. theotyflos
    Replies:
    3
    Views:
    464
    Thomas Matthews
    Feb 19, 2004
  2. Nikita Petrov
    Replies:
    2
    Views:
    109
    Gary Wright
    Apr 6, 2008
  3. Kenneth McDonald
    Replies:
    5
    Views:
    311
    Kenneth McDonald
    Sep 26, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    105
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    111
    PerlFAQ Server
    Feb 17, 2011
Loading...

Share This Page