[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is there an easy way to extend an object?

Jeff

12/28/2005 1:40:00 AM

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

--
Posted via http://www.ruby-....


5 Answers

dblack

12/28/2005 1:45:00 AM

0

Jeff

12/28/2005 1:56:00 AM

0

unknown wrote:
> You can directly define a method on a particular object:
>
> def obj.new_method
> ...
> end
>

That worked great! Thanks.

Jeff

--
Posted via http://www.ruby-....


Dan Shafer

12/28/2005 7:16:00 AM

0

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?

On Dec 27, 2005, at 5:40 PM, Jeff Cohen wrote:

> 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
>
> --
> Posted via http://www.ruby-....
>



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




Robert Klemme

12/28/2005 1:42:00 PM

0

Jeff Cohen <cohen.jeff@gmail.com> wrote:
> unknown wrote:
>> You can directly define a method on a particular object:
>>
>> def obj.new_method
>> ...
>> end
>>
>
> 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:

>> require 'delegate'
=> true
>> s="foo"
=> "foo"
>> o=SimpleDelegater.new s
=> "foo"
>> o.length
=> 3
>> def o.foo() length * 2 end
=> nil
>> o.foo
=> 6
>> o << "bar"
=> "foobar"
>> o.length
=> 6
>> o.foo
=> 12

Kind regards

robert

Jeff

12/28/2005 4:07:00 PM

0

Robert Klemme wrote:
> 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

--
Posted via http://www.ruby-....