Confusion with constant look up

Xavier N. wrote in post #1122791:

(and some obscure use cases of the *_eval family).

Can you one or two examples to understand the above sentence? I don’t
understand what do you mean?

The module keywords above push the module object to the stack. Note
that I said the module object, nesting has nothing to do with constants.
In

So when we use class keyword then also it is pushed to the Stack like
module objects. Am I right ?

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::b::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]

Xavier

Xavier N. wrote in post #1123075:

On Tue, Oct 1, 2013 at 9:21 PM, Love U Ruby [email protected]
wrote:

The constants API and nesting are totally different concepts.
and Module#constants here:

http://ruby-doc.org/core-2.0.0/Module.html#method-i-constants

Okay! I tried the code below :

module A
module B
Z = 2
end
X=10
end
module A::B
module C
Y =10
end
end
A.constants

=> [:B, :X]

A::b::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] ?