On Oct 28, 10:39=A0am, Robert Klemme <shortcut... / googlemail.com> wrote:
> On Fri, Oct 28, 2011 at 3:40 PM, Intransition <transf... / gmail.com> wrote=
:
> > Can it be done?
>
> > E.g.
>
> > =A0o =3D BasicObject.new
> > =A0o.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
> =3D> #<UnboundMethod: Object(Kernel)#extend>
>
> And since
>
> irb(main):002:0> Object < Kernel
> =3D> true
> irb(main):003:0> BasicObject < Kernel
> =3D> 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
> =A0 =A0 =A0 =A0 from (irb):19:in `bind'
> =A0 =A0 =A0 =A0 from (irb):19
> =A0 =A0 =A0 =A0 from /opt/bin/irb19:12:in `<main>'
>
> 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<<self; include ::Enumerable; end
> irb(main):013:2> end
> irb(main):014:1> end
> =3D> nil
> irb(main):015:0> x=3DX.new
> (Object doesn't support #inspect)
> =3D>
> irb(main):016:0> Enumerable =3D=3D=3D x
> =3D> 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.