[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

double extend of object

ara.t.howard

5/21/2007 10:03:00 PM


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
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




2 Answers

Joel VanderWerf

5/21/2007 10:16:00 PM

0

ara.t.howard wrote:
>
> 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]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

ara.t.howard

5/21/2007 10:23:00 PM

0


On May 21, 2007, at 4:15 PM, Joel VanderWerf wrote:

> ara.t.howard wrote:
>> 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
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama