# Extending object with another object

**URL:** <https://rubytalk.org/t/extending-object-with-another-object/49771>\
**Category:** ruby-talk\
**Created:** [22 October 2008 07:52 UTC](https://rubytalk.org/t/extending-object-with-another-object/49771 "2008-10-22T07:52:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ittay\_Dror](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@Ittay\_Dror](https://rubytalk.org/u/Ittay_Dror)\
**Post date:** [22 October 2008 07:52 UTC](https://rubytalk.org/t/extending-object-with-another-object/49771/1 "2008-10-22T07:52:38Z")

</div>

Hi,

How is it possible to extend an object using another object?

I know I can iterate the public methods of one and define them (or maybe clone, and bind) in the other, but I'm looking for something that will work more closely to how 'extend' works.

Thanks,

Ittay

> **···**
>
> --  
> --  
> Ittay Dror \<[ittay.dror@gmail.com](mailto:ittay.dror@gmail.com)\>

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [22 October 2008 08:20 UTC](https://rubytalk.org/t/extending-object-with-another-object/49771/2 "2008-10-22T08:20:22Z")

</div>

Ittay Dror wrote:

> How is it possible to extend an object using another object?

It depends exactly on what you're trying to achieve.

You have two separate objects, both with their own sets of methods \*and\*  
their own sets of instance variables. In what way do you want to  
"extend" the first with the second? What happens to instance variables  
belonging to the second object?

Now, if you simply want a call to method X on object A to be forwarded  
to object B (in which context it would use object B's instance  
variables), then look at delegate.rb, or you can synthesise this pattern  
yourself using method\_missing

class A  
&nbsp;&nbsp;def hello  
&nbsp;&nbsp;&nbsp;&nbsp;puts "hello"  
&nbsp;&nbsp;end  
end

class B  
&nbsp;&nbsp;def bye  
&nbsp;&nbsp;&nbsp;&nbsp;puts "bye"  
&nbsp;&nbsp;end  
end

a = A.new  
b = B.new

# This is a rough implementation of SimpleDelegator  
def a.other=(x)  
&nbsp;&nbsp;@other=x  
end  
def a.method\_missing(\*args)  
&nbsp;&nbsp;@other.send(\*args)  
end

a.other = b  
a.hello  
a.bye

The important thing here is that when methods are delegated to the other  
object, they will be in the context of that object and hence have access  
to that object's instance variables. Put simply, that object will  
"work".

In cases where the methods by themselves make sense, then generally  
you'd stick those methods into a Module so that both a and b could make  
use of them.

class A  
&nbsp;&nbsp;include MyStuff  
end  
...  
b = B.new  
b.extend MyStuff

But clearly you know about this already.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [22 October 2008 19:17 UTC](https://rubytalk.org/t/extending-object-with-another-object/49771/3 "2008-10-22T19:17:23Z")

</div>

You have to use delegation. Look up delegate.rb.

T.

> **···**
>
> On Oct 22, 3:52 am, Ittay Dror \<ittay.d...@gmail.com\> wrote:
> 
> > Hi,
> > 
> > How is it possible to extend an object using another object?
> > 
> > I know I can iterate the public methods of one and define them (or maybe  
> > clone, and bind) in the other, but I'm looking for something that will  
> > work more closely to how 'extend' works.

---

<div class="post-metadata">

**Author:** ![Ittay\_Dror](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@Ittay\_Dror](https://rubytalk.org/u/Ittay_Dror)\
**Post date:** [22 October 2008 09:38 UTC](https://rubytalk.org/t/extending-object-with-another-object/49771/4 "2008-10-22T09:38:54Z")

</div>

Brian Candler wrote:

> Ittay Dror wrote:  
> &nbsp;&nbsp;
> 
> > How is it possible to extend an object using another object?  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > It depends exactly on what you're trying to achieve.
> 
> You have two separate objects, both with their own sets of methods \*and\* their own sets of instance variables. In what way do you want to "extend" the first with the second? What happens to instance variables belonging to the second object?  
> &nbsp;&nbsp;

I want just to get the methods defined in the second object, something like  
object.extend(as\_module(other\_object))

where 'as\_module' is some method that returns a module representing the class hierarchy (with singletons) of the other object.

class A  
&nbsp;&nbsp;def initialize(name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@name = name  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def hi  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts @name  
&nbsp;&nbsp;end  
end

class B  
&nbsp;&nbsp;def initialize(name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@name = name  
&nbsp;&nbsp;end  
end

a = A.new('a')  
b = B.new('b')  
b.object\_extend(a)  
b.hi #=\> 'b'

Ittay

> **···**
>
> > Now, if you simply want a call to method X on object A to be forwarded to object B (in which context it would use object B's instance variables), then look at delegate.rb, or you can synthesise this pattern yourself using method\_missing
> > 
> > class A  
> > &nbsp;&nbsp;def hello  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "hello"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class B  
> > &nbsp;&nbsp;def bye  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "bye"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > a = A.new  
> > b = B.new
> > 
> > # This is a rough implementation of SimpleDelegator  
> > def a.other=(x)  
> > &nbsp;&nbsp;@other=x  
> > end  
> > def a.method\_missing(\*args)  
> > &nbsp;&nbsp;@other.send(\*args)  
> > end
> > 
> > a.other = b  
> > a.hello  
> > a.bye
> > 
> > The important thing here is that when methods are delegated to the other object, they will be in the context of that object and hence have access to that object's instance variables. Put simply, that object will "work".
> > 
> > In cases where the methods by themselves make sense, then generally you'd stick those methods into a Module so that both a and b could make use of them.
> > 
> > class A  
> > &nbsp;&nbsp;include MyStuff  
> > end  
> > ...  
> > b = B.new  
> > b.extend MyStuff
> > 
> > But clearly you know about this already.  
> > &nbsp;&nbsp;  
> > --  
> > Ittay Dror \<ittayd@tikalk.com\>  
> > Tikal \<[http://www.tikalk.com](http://www.tikalk.com)\>  
> > Tikal Project \<[http://tikal.sourceforge.net](http://tikal.sourceforge.net)\>
> 
> --  
> --  
> Ittay Dror \<ittay.dror@gmail.com\>

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [22 October 2008 18:32 UTC](https://rubytalk.org/t/extending-object-with-another-object/49771/5 "2008-10-22T18:32:34Z")

</div>

Ittay Dror wrote:

> Brian Candler wrote:
> 
> > belonging to the second object?  
> > &nbsp;&nbsp;
> 
> I want just to get the methods defined in the second object, something  
> like  
> object.extend(as\_module(other\_object))
> 
> where 'as\_module' is some method that returns a module representing the  
> class hierarchy (with singletons) of the other object.

But I still don't know what the \*purpose\* of this is, i.e. what actual  
problem you're trying to solve, and without that I can't propose a  
better solution.

What you're asking for is not especially easy to do. I guess you could  
iterate over the methods of B, obtain UnboundMethod objects, and bind  
them to A. But I've never come across any situation where I've needed  
(or wanted) to do this.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
