On Tue, Oct 1, 2013 at 9:52 PM, Love U Ruby [email protected]
wrote:
Xavier N. wrote in post #1123075:
module A
A.constants
=> [:B, :X]
A::C::Y # => 10
It seems Module#constants - which Returns an array of the names of the
constants accessible in mod A. Not from the nested modules. Now my
questions is - Can we do something by which A.constants can output as
[:B, :X,:Z,:C,:Y] ?
There is no builtin way to do this, but you can easily program it:
def recursive_constants(mod)
constants = []
mod.constants.each do |constant|
constants << constant
value = mod.const_get(constant)
constants += recursive_constants(value) if value.is_a?(Module)
end
constants
end
module A
module B
Z = 2
end
X=10
end
module A::B
module C
Y =10
end
end
recursive_constants(A) # => [:B, :Z, :C, :Y, :X]
module A
module B
Z = 2
end
X=10
end
module A::B
module C
Y =10
end
end
A.constants
=> [:B, :X]
A::C::Y # => 10
It seems Module#constants - which Returns an array of the names of the
constants accessible in mod A. Not from the nested modules. Now my
questions is - Can we do something by which A.constants can output as
[:B, :X,:Z,:C,:Y] ?