How to get the extended module list?

module A
def meth1;end
def meth2;end
end

module B
def meth11;end
def meth21;end
end

class C
include A
extend B
end

p C.included_modules # => [A, Kernel]

We do have Module#included_modules methods to get the list of included
modules. But how to get the extended modules list for C?

On Thu, Nov 21, 2013 at 4:42 PM, Love U Ruby remo[email protected]
wrote:

class C
include A
extend B
end

p C.included_modules # => [A, Kernel]

We do have Module#included_modules methods to get the list of included
modules. But how to get the extended modules list for C?

irb(main):015:0> C.singleton_class.ancestors
=> [B, Class, Module, Object, Kernel, BasicObject]
irb(main):016:0> C.singleton_class.included_modules
=> [B, Kernel]

Cheers

robert

Robert K. wrote in post #1128179:

On Thu, Nov 21, 2013 at 4:42 PM, Love U Ruby [email protected]
wrote:

class C
include A
extend B
end

p C.included_modules # => [A, Kernel]

We do have Module#included_modules methods to get the list of included
modules. But how to get the extended modules list for C?

irb(main):015:0> C.singleton_class.ancestors
=> [B, Class, Module, Object, Kernel, BasicObject]
irb(main):016:0> C.singleton_class.included_modules
=> [B, Kernel]

Excellent!! Robert… Does it mean singleton_class contains always
extended modules names ? Could you explain me the logic? I am missing
the connection.

Ignore my code,as I don’t think it is good code. I was writing my own
code.

I wrote this one :

MODULE_LIST = Hash.new{|hsh,key| hsh[key] = []}

module A
def self.extended(mod)
MODULE_LIST[mod] << self
end
def meth1;end
def meth2;end
end

module B
def self.extended(mod)
MODULE_LIST[mod] << self
end
def meth11;end
def meth21;end
end

class C
extend B
def self.extended_modules
MODULE_LIST
end
end

p C.extended_modules # => {C=>[B]}

On Thu, Nov 21, 2013 at 5:03 PM, Love U Ruby [email protected]
wrote:

We do have Module#included_modules methods to get the list of included
modules. But how to get the extended modules list for C?

irb(main):015:0> C.singleton_class.ancestors
=> [B, Class, Module, Object, Kernel, BasicObject]
irb(main):016:0> C.singleton_class.included_modules
=> [B, Kernel]

Excellent!! Robert… Does it mean singleton_class contains always
extended modules names ?

Yes.

Could you explain me the logic? I am missing
the connection.

irb(main):001:0> o = Object.new
=> #Object:0x0000060004b9c8
irb(main):002:0> o.singleton_class.included_modules
=> [Kernel]
irb(main):003:0> o.extend Enumerable
=> #Object:0x0000060004b9c8
irb(main):004:0> o.singleton_class.included_modules
=> [Enumerable, Kernel]

irb(main):005:0> o = Object.new
=> #Object:0x000006003f38f0
irb(main):006:0> o.singleton_class.included_modules
=> [Kernel]
irb(main):007:0> class <<o; include Enumerable; end
=> #<Class:#Object:0x000006003f38f0>
irb(main):008:0> o.singleton_class.included_modules
=> [Enumerable, Kernel]

irb(main):009:0> o = Object.new
=> #Object:0x000006003534e0
irb(main):010:0> o.singleton_class.included_modules
=> [Kernel]
irb(main):011:0> o.singleton_class.class_eval { include Enumerable }
=> #<Class:#Object:0x000006003534e0>
irb(main):012:0> o.singleton_class.included_modules
=> [Enumerable, Kernel]

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs