# Extending object with another object

**URL:** <https://www.ruby-forum.com/t/extending-object-with-another-object/151508>\
**Category:** Ruby\
**Created:** [October 22, 2008, 9:54am UTC](https://www.ruby-forum.com/t/extending-object-with-another-object/151508 "2008-10-22T09:54:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [October 22, 2008, 9:54am UTC](https://www.ruby-forum.com/t/extending-object-with-another-object/151508/1 "2008-10-22T09:54:39Z")

</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

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [October 22, 2008, 10:21am UTC](https://www.ruby-forum.com/t/extending-object-with-another-object/151508/2 "2008-10-22T10:21:52Z")

</div>

Ittay D. 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  
def hello  
puts “hello”  
end  
end

class B  
def bye  
puts “bye”  
end  
end

a = A.new  
b = B.new

# This is a rough implementation of SimpleDelegator

def a.other=(x)  
@other=x  
end  
def a.method\_missing(\*args)  
@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  
include MyStuff  
end  
…  
b = B.new  
b.extend MyStuff

But clearly you know about this already.

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [October 22, 2008, 11:41am UTC](https://www.ruby-forum.com/t/extending-object-with-another-object/151508/3 "2008-10-22T11:41:31Z")

</div>

Brian C. wrote:

> belonging to the second object?

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  
def initialize(name)  
@name = name  
end  
def hi  
puts @name  
end  
end

class B  
def initialize(name)  
@name = name  
end  
end

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

Ittay

> def a.other=(x)  
> The important thing here is that when methods are delegated to the other  
> end  
> …  
> b = B.new  
> b.extend MyStuff
> 
> But clearly you know about this already.

–  
Ittay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Tikal [http://www.tikalk.com](http://www.tikalk.com)  
Tikal Project [http://tikal.sourceforge.net](http://tikal.sourceforge.net)

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [October 22, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/extending-object-with-another-object/151508/4 "2008-10-22T20:34:09Z")

</div>

Ittay D. wrote:

> Brian C. wrote:
> 
> > belonging to the second object?
> 
> 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.

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [October 22, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/extending-object-with-another-object/151508/5 "2008-10-22T21:19:56Z")

</div>

On Oct 22, 3:52 am, Ittay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

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

T.
