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,

oof) {'I say poof!'}
x =3D MyClass.new
y =3D MyClass.new
x.define_singleton_method(

oof_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,
oof) {'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: