# Using a class to extend a singleton?

**URL:** <https://www.ruby-forum.com/t/using-a-class-to-extend-a-singleton/130686>\
**Category:** Ruby\
**Created:** [March 1, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/using-a-class-to-extend-a-singleton/130686 "2008-03-01T22:29:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/using-a-class-to-extend-a-singleton/130686/1 "2008-03-01T22:29:19Z")

</div>

Given:

class X  
def yo=x; @yo=x; end  
end

class Y  
def yo!; puts @yo; end  
end

x = X.new

Is there any way to extend x with Y’s functionality (without going  
back and changing the given code)? Eg.

> > x.yo=“Hello World”  
> > x.yo!  
> > Hello World

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 1, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/using-a-class-to-extend-a-singleton/130686/2 "2008-03-01T22:41:42Z")

</div>

On 01/03/2008, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> x = X.new
> 
> Is there any way to extend x with Y’s functionality (without going  
> back and changing the given code)? Eg.
> 
> > > x.yo=“Hello World”  
> > > x.yo!  
> > > Hello World
> 
> T.

class \<\< x; Y.instance\_methods.each {|m| define\_method m, Y.method(:m)};  
end

kind of. brings in Object onwards, though, but that’s easy to fix.

-ehird

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 2, 2008, 7:36am UTC](https://www.ruby-forum.com/t/using-a-class-to-extend-a-singleton/130686/3 "2008-03-02T07:36:47Z")

</div>

On Mar 1, 4:40 pm, “Elliott H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > class Y  
> > Hello World
> 
> > T.
> 
> class \<\< x; Y.instance\_methods.each {|m| define\_method m, Y.method(:m)}; end
> 
> kind of. brings in Object onwards, though, but that’s easy to fix.

Almost but not quite. You’d have to use Y.instance\_method(:m) instead,  
but then you get an error anyway because Y is not a subclass of X or  
something to that effect.

I don’t think there is any reasonable way. It’s too bad. After the  
discussion on MP, I decided to take a second shot at better  
encapsulation of Facets. One idea was to just have a special way to  
load them --if I could do the above that would work. But no go there  
and nfortunately the traditional module namespaces doesn’t work well  
either --they can’t handle aliasing of built-in methods and the double  
module inclusion problem means it won’t work for modules either.

Ruby really needs some fixing in this area!

To clarify the problem a faux example:

module Facets  
module Comparable  
alias\_method :gte, :\>= # no method to alias  
def ccc; “ccc”; end  
end  
end

module Comparable  
include Facets::Comparable  
end

10.ccc #=\> NoMethodError

T.
