Is there an easy way to extend an object?

J

Jeff Cohen

Given an object, is there a clean way of creating a new object that
wraps the original and then adds a new method?

It doesn't have to wrap the old object if there's a way to directly add
a new method to the object (I'm allowed to change the incoming object).

def extend_it(obj)

# how do I add a method to obj
# or create a wrapper for it?

return obj #or new_obj that quacks like obj, has obj data, and also
has extra method

end

In my C++ days I would create a class that derives from the original
class, extends it with a new method, and defines a copy constructor to
copy the state of the original object.

Thanks
Jeff
 
D

dblack

Hi --

Given an object, is there a clean way of creating a new object that
wraps the original and then adds a new method?

It doesn't have to wrap the old object if there's a way to directly add
a new method to the object (I'm allowed to change the incoming object).

def extend_it(obj)

# how do I add a method to obj
# or create a wrapper for it?

You can directly define a method on a particular object:

def obj.new_method
...
end

You can also create a module, and then use 'extend' to add that
module's instance method's to the object's capabilities.

module M
def x
end
end
obj.extend(M)

There are a few more variants on these if they're not what you
need....


David

--
David A. Black
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
D

Dan Shafer

Jeff.....

In a more-or-less "pure" OO language like Ruby (or Smalltalk), you
don't have to go through all those gyrations that C++ (and other
languages that added OO after the fact) forced us to use. You just
add a method to the existing object because classes are first-class
objects.

Cool, eh?

Given an object, is there a clean way of creating a new object that
wraps the original and then adds a new method?

It doesn't have to wrap the old object if there's a way to directly
add
a new method to the object (I'm allowed to change the incoming
object).

def extend_it(obj)

# how do I add a method to obj
# or create a wrapper for it?

return obj #or new_obj that quacks like obj, has obj data, and also
has extra method

end

In my C++ days I would create a class that derives from the original
class, extends it with a new method, and defines a copy constructor to
copy the state of the original object.

Thanks
Jeff



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
Dan Shafer
Technology Visionary - Technology Assessment - Documentation
"Looking at technology from every angle"
http://www.eclecticity.com
 
R

Robert Klemme

Jeff Cohen said:
That worked great! Thanks.

Jeff

Just for the sake of completeness: if you are not allowed to modify the
original instance or do not want to do it there's a delegator module:
=> 12

Kind regards

robert
 
J

Jeff Cohen

Robert said:
Just for the sake of completeness: if you are not allowed to modify the
original instance or do not want to do it there's a delegator module:

Now that is also really cool. Ruby is my first "dynamic" language and
I'm starting to see how powerful it can be.

Thanks for the tip.

Jeff
www.softiesonrails.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top