Use #extend on BasicObject?

Can it be done?

E.g.

o = BasicObject.new
o.extend Enumerable

(Don’t take this specific example for what I am trying to do. It’s
just an easy example off the top of my head.)

no it does not work, extern only works on Object and subclasses

On Fri, Oct 28, 2011 at 3:40 PM, Intransition [email protected]
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
=> #<UnboundMethod: Object(Kernel)#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<<self; include ::Enumerable; end
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

Kind regards

robert

On Oct 28, 10:39am, Robert K. [email protected] wrote:

you cannot bind method extend to an instance of 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
=> 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 :slight_smile:

However Nakada gave me a bit nicer one I think:

Enumerable.send(:extend_object, BasicObject.new)

Using the module callback. pretty smart.

On Fri, Oct 28, 2011 at 5:13 PM, Intransition [email protected]
wrote:

On Oct 28, 10:39am, Robert K. [email protected] wrote:

On Fri, Oct 28, 2011 at 3:40 PM, Intransition [email protected] wrote:

Thanks. Robert. You are a reliable work horse on this mailing list.
The community owes you much kudo.

Thank you!

I jumped the gun and put in a feature request on redmine. matz gave me
the same answer. so you are in good company :slight_smile:

:slight_smile:

However Nakada gave me a bit nicer one I think:

Enumerable.send(:extend_object, BasicObject.new)

Using the module callback. pretty smart.

Indeed! Still it’s more verbose than it should be, don’t you think?
Making this method public seems like the idea solution. Do you have
any insight / idea why it is private?

Kind regards

robert

On Oct 28, 5:59pm, Robert K. [email protected] wrote:

the same answer. so you are in good company :slight_smile:
Making this method public seems like the idea solution. Do you have
any insight / idea why it is private?

Probably b/c normally you’re not supposed to call it directly. You use
object#extend(module) instead and then that dispatches to
module.extend_object(object). In other words, it’s private as form of
deterrence from casual use.