Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > using send to define object methods

Reply
Thread Tools

using send to define object methods

 
 
David Miller
Guest
Posts: n/a
 
      09-18-2010
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, oof) {'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/.

 
Reply With Quote
 
 
 
 
Y. NOBUOKA
Guest
Posts: n/a
 
      09-18-2010
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 <(E-Mail Removed)>:
> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      09-18-2010
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, oof) {'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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.76 How do I define methods for every class/object? PerlFAQ Server Perl Misc 0 02-17-2011 11:00 AM
FAQ 4.76 How do I define methods for every class/object? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Using object methods of first module in methods of second module Nikita Petrov Ruby 2 04-06-2008 08:49 PM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments