Forum: Ruby double extend of object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2007-05-22 00:04
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-05-22 00:16
(Received via mailing list)
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]
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2007-05-22 00:23
(Received via mailing list)
On May 21, 2007, at 4:15 PM, Joel VanderWerf wrote:

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

yes ;-)

-a
This topic is locked and can not be replied to.