From: Intransition Date: 2011-10-29T00:13:48+09:00 Subject: Re: Use #extend on BasicObject ? On Oct 28, 10:39 am, Robert Klemme wrote: > On Fri, Oct 28, 2011 at 3:40 PM, Intransition wrote: > > Can it be done? > > > E.g. > > >  o = BasicObject.new > >  o.extend Enumerable > > Funnily enough I stumbled across the very same issue a few days ago. > The reason you can't #extend is > > irb(main):001:0> Object.instance_method :extend > => # > > And since > > irb(main):002:0> Object < Kernel > => true > irb(main):003:0> BasicObject < Kernel > => nil > > you cannot bind method extend to an instance of BasicObject: > > irb(main):019:0> Object.instance_method(:extend).bind(BasicObject.new) > TypeError: bind argument must be an instance of Object >         from (irb):19:in `bind' >         from (irb):19 >         from /opt/bin/irb19:12:in `
' > > There is just one way I am aware of: > > irb(main):010:0> class X < BasicObject > irb(main):011:1> def initialize > irb(main):012:2> class< irb(main):013:2> end > irb(main):014:1> end > => nil > irb(main):015:0> x=X.new > (Object doesn't support #inspect) > => > irb(main):016:0> Enumerable === x > => true Thanks. Robert. You are a reliable work horse on this mailing list. The community owes you much kudo. I jumped the gun and put in a feature request on redmine. matz gave me the same answer. so you are in good company :-) However Nakada gave me a bit nicer one I think: Enumerable.__send__(:extend_object, BasicObject.new) Using the module callback. pretty smart.