7stud2
(7stud --)
21 November 2013 15:42
1
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?
···
--
Posted via http://www.ruby-forum.com/ .
Robert_K1
(Robert K.)
21 November 2013 15:48
2
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
···
On Thu, Nov 21, 2013 at 4:42 PM, Love U Ruby <lists@ruby-forum.com> wrote:
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?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
7stud2
(7stud --)
21 November 2013 16:03
3
Robert Klemme wrote in post #1128179:
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 4:42 PM, Love U Ruby <lists@ruby-forum.com> > wrote:
--
Posted via http://www.ruby-forum.com/\ .
Robert_K1
(Robert K.)
21 November 2013 17:54
4
Robert Klemme wrote in post #1128179:
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 ?
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
···
On Thu, Nov 21, 2013 at 5:03 PM, Love U Ruby <lists@ruby-forum.com> wrote:
On Thu, Nov 21, 2013 at 4:42 PM, Love U Ruby <lists@ruby-forum.com> >> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/