# Issue extending a delegated class

**URL:** <https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088>\
**Category:** Ruby\
**Created:** [July 27, 2006, 2:19am UTC](https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088 "2006-07-27T02:19:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rak\_rok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@rak\_rok](https://www.ruby-forum.com/u/rak_rok)\
**Post date:** [July 27, 2006, 2:19am UTC](https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088/1 "2006-07-27T02:19:24Z")

</div>

Hi,

I have a simple class that I’m extending via delegation:

class A  
def one ; puts “A::one” ; two ; end  
def two ; puts “A::two” ; end  
end

class B \< SimpleDelegator

def initialize  
a = A.new

```
# do some stuff to a

__setobj__ a
super( a )

```

end  
end

I then instantiate B, and use that, all is well. However, i’m running  
into  
issues whenever I try to override a method in B:  
b = B.new  
class \<\< b  
def two ; puts “foo” ; end  
end

I would have expected b.one to output:  
A::one  
custom

But instead i get:  
A::one  
A::two

However, calling the overriden method directly produces the expected  
result. I’m guessing the extend is really only adding the function to  
the B  
instance and not to A? Is there a way to get to A?

Thanks,  
-rak-

---

<div class="post-metadata">

**Author:** ![rak\_rok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@rak\_rok](https://www.ruby-forum.com/u/rak_rok)\
**Post date:** [July 27, 2006, 2:50am UTC](https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088/2 "2006-07-27T02:50:17Z")

</div>

Hi –

On Thu, 27 Jul 2006, rak rok wrote:

> I then instantiate B, and use that, all is well. However, i’m running into  
> issues whenever I try to override a method in B:  
> b = B.new  
> class \<\< b

You’re not overriding anything in B here; rather, you’re adding an  
instance method to the singleton class of the instance b.

> def two ; puts “foo” ; end  
> end
> 
> I would have expected b.one to output:  
> A::one  
> custom

(Do you mean “foo” rather than “custom”?)

> But instead i get:  
> A::one  
> A::two
> 
> However, calling the overriden method directly produces the expected  
> result. I’m guessing the extend is really only adding the function to the B  
> instance and not to A? Is there a way to get to A?

Your guess is right 🙂 When a handles the delegate call to #one,  
part of that call is a call to #two – which, as far as a is  
concerned, is A#two.

You can always override A#two, but I imagine that’s not what you have  
in mind. I’m not sure exactly what you are trying to do, though.

David

---

<div class="post-metadata">

**Author:** ![rak\_rok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@rak\_rok](https://www.ruby-forum.com/u/rak_rok)\
**Post date:** [July 27, 2006, 6:24am UTC](https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088/3 "2006-07-27T06:24:52Z")

</div>

Hi David,

> > I would have expected b.one to output:  
> > A::one  
> > custom
> 
> (Do you mean “foo” rather than “custom”?)

(Woops, yes i meant “foo”)

> Your guess is right 🙂 When a handles the delegate call to #one,  
> part of that call is a call to #two – which, as far as a is  
> concerned, is A#two.
> 
> You can always override A#two, but I imagine that’s not what you have  
> in mind. I’m not sure exactly what you are trying to do, though.

Makes sense… But then how is it possible to override A#two if all I  
have is an instance of B?

Thanks!  
-rak-

---

<div class="post-metadata">

**Author:** ![rak\_rok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@rak\_rok](https://www.ruby-forum.com/u/rak_rok)\
**Post date:** [July 27, 2006, 6:37am UTC](https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088/4 "2006-07-27T06:37:03Z")

</div>

Hi –

On Thu, 27 Jul 2006, rak rok wrote:

> > Your guess is right 🙂 When a handles the delegate call to #one,  
> > part of that call is a call to #two – which, as far as a is  
> > concerned, is A#two.
> > 
> > You can always override A#two, but I imagine that’s not what you have  
> > in mind. I’m not sure exactly what you are trying to do, though.
> 
> Makes sense… But then how is it possible to override A#two if all I  
> have is an instance of B?

You really want b’s delegated object to have a new #two – so:

class \<\< b. **getobj**  
def two  
puts “custom”  
end  
end

or something along those lines.

David

---

<div class="post-metadata">

**Author:** ![rak\_rok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@rak\_rok](https://www.ruby-forum.com/u/rak_rok)\
**Post date:** [July 27, 2006, 6:46am UTC](https://www.ruby-forum.com/t/issue-extending-a-delegated-class/100088/5 "2006-07-27T06:46:20Z")

</div>

> You really want b’s delegated object to have a new #two – so:
> 
> class \<\< b. **getobj**  
> def two  
> puts “custom”  
> end  
> end

Awesome, I didn’t know about **getobj** , thanks a lot.

-rak-

PS. Thanks for a great book!
