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.
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.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
Dan S.
Technology Visionary - Technology Assessment - Documentation
“Looking at technology from every angle”
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.