double extend of object

A

ara.t.howard

does anyone know if this can be relied upon?

cfp:~ > cat a.rb
class C
def singleton_class &b
sc =
class << self;
self
end
b ? sc.instance_eval(&b) : sc
end
def extend m
ancestors = [singleton_class.ancestors,
self.class.ancestors].flatten.uniq
super(ancestors.index(m) ? m.dup : m)
end
end

module A
def foo() 'A' end
end

module B
def foo() 'B' end
end

c = C.new

c.extend A
p c.foo

c.extend B
p c.foo

c.extend A
p c.foo


cfp:~ > ruby a.rb
"A"
"B"
"A"


??


-a
 
J

Joel VanderWerf

ara.t.howard said:
does anyone know if this can be relied upon?

Is this the part you are really asking about?

module M; end

c = Object.new

c.extend M
c.extend M.dup

class << c; p ancestors; end
# ==> [#<Module:0xb7cf26ac>, M, Object, Kernel]
 
A

ara.t.howard

ara.t.howard said:
does anyone know if this can be relied upon?

Is this the part you are really asking about?

module M; end

c = Object.new

c.extend M
c.extend M.dup

class << c; p ancestors; end
# ==> [#<Module:0xb7cf26ac>, M, Object, Kernel]

yes ;-)

-a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top