Forum: Ruby Is there an easy way to extend an object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2005-12-28 02:40
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2005-12-28 02:47
(Received via mailing list)
Hi --

On Wed, 28 Dec 2005, 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?

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
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2005-12-28 02:55
unknown wrote:
> You can directly define a method on a particular object:
>
>    def obj.new_method
>      ...
>    end
>

That worked great!  Thanks.

Jeff
205bc8d44e9bc5c68d77dd412abcb3ce?d=identicon&s=25 Dan Shafer (Guest)
on 2005-12-28 08:18
(Received via mailing list)
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:

>   # how do I add a method to obj
>
> Thanks
> Jeff
>
> --
> Posted via http://www.ruby-forum.com/.
>



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
Dan Shafer
Technology Visionary - Technology Assessment - Documentation
"Looking at technology from every angle"
http://www.eclecticity.com
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2005-12-28 14:43
(Received via mailing list)
Jeff Cohen <cohen.jeff@gmail.com> wrote:
> 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
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2005-12-28 17:07
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
This topic is locked and can not be replied to.