# Is there an easy way to extend an object?

**URL:** <https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209>\
**Category:** Ruby\
**Created:** [December 28, 2005, 2:40am UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209 "2005-12-28T02:40:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 28, 2005, 2:40am UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209/1 "2005-12-28T02:40:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 28, 2005, 2:47am UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209/2 "2005-12-28T02:47:16Z")

</div>

Hi –

On Wed, 28 Dec 2005, Jeff C. 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  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 28, 2005, 2:55am UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209/3 "2005-12-28T02:55:50Z")

</div>

unknown wrote:

> You can directly define a method on a particular object:
> 
> def obj.new\_method  
> …  
> end

That worked great! Thanks.

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 28, 2005, 2:43pm UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209/4 "2005-12-28T14:43:23Z")

</div>

Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

```

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 28, 2005, 5:07pm UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209/5 "2005-12-28T17:07:08Z")

</div>

Robert K. 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

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 28, 2005, 8:18am UTC](https://www.ruby-forum.com/t/is-there-an-easy-way-to-extend-an-object/51209/6 "2005-12-28T08:18:04Z")

</div>

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 C. wrote:

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

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.  
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-  
Dan S.  
Technology Visionary - Technology Assessment - Documentation  
“Looking at technology from every angle”

> **[Eclecticity.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=eclecticity.com)**
>
> Shop a wide selection of domains at HugeDomains.com. Find the right domain name today.
