From: Trans Date: 2008-03-02T15:35:50+09:00 Subject: Re: Using a class to extend a singleton? On Mar 1, 4:40 pm, "Elliott Hird" wrote: > On 01/03/2008, Trans wrote: > > > > > 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. > > 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.