# Enumerating constants in anonymous modules

**URL:** <https://www.ruby-forum.com/t/enumerating-constants-in-anonymous-modules/150537>\
**Category:** Ruby\
**Created:** [October 9, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/enumerating-constants-in-anonymous-modules/150537 "2008-10-09T17:24:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 9, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/enumerating-constants-in-anonymous-modules/150537/1 "2008-10-09T17:24:19Z")

</div>

How can I enumerate all the constants inside the module in the second  
example?  
Notice that #constants array is empty

For bonus points, which is the “correct” eval method to use to define a  
class inside the module (they all seem to work)

Cheers,  
Xavier

module Foo  
class A  
end  
end

puts Foo.constants.inspect # =\> [“A”]

[:module\_eval, :instance\_eval, :class\_eval].each do |method|  
m = Module.new  
m.send(method) do  
class B  
end  
end  
puts m.constants.inspect # =\> [] (I would expect [‘B’])  
puts m.const\_get(‘B’).inspect # =\> B  
end

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 9, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/enumerating-constants-in-anonymous-modules/150537/2 "2008-10-09T17:57:36Z")

</div>

Hi –

On Fri, 10 Oct 2008, Xavier S. wrote:

> How can I enumerate all the constants inside the module in the second  
> example?  
> Notice that #constants array is empty

See comments in code, below.

> For bonus points, which is the “correct” eval method to use to define a  
> class inside the module (they all seem to work)

module\_eval and class\_eval are synonyms for each other. I’d use  
module\_eval over instance\_eval, unless you specifically need instance  
methods to be singleton methods on the module object (which is  
unlikely, and can be achieved by other means under module\_eval).

> class B  
> The constant B is resolved in the usual “semi-static”, eager way,  
> which means it does not belong to the new module: it’s actually being  
> defined at the top level.

> end  
> end  
> puts m.constants.inspect # =\> [] (I would expect [‘B’])  
> puts m.const\_get(‘B’).inspect # =\> B  
> end

Try this:

m = Module.new  
m.module\_eval do  
class self::B # forces Ruby to wait to bind the constant  
end  
end  
puts m.constants.inspect # [“B”]  
puts m.const\_get(‘B’).inspect # #Module:0x220164::B

David

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 9, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/enumerating-constants-in-anonymous-modules/150537/3 "2008-10-09T18:05:49Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Fri, 10 Oct 2008, Xavier S. wrote:
> 
> > How can I enumerate all the constants inside the module in the second  
> > example?  
> > Notice that #constants array is empty
> 
> See comments in code, below.
> 
> > For bonus points, which is the “correct” eval method to use to define a  
> > class inside the module (they all seem to work)
> 
> module\_eval and class\_eval are synonyms for each other. I’d use  
> module\_eval over instance\_eval, unless you specifically need instance  
> methods to be singleton methods on the module object (which is  
> unlikely, and can be achieved by other means under module\_eval).

I’m not really sure what I need, I’ll do some more reading and come back  
if I have any further questions. Using module\_eval for now.

> Try this:
> 
> m = Module.new  
> m.module\_eval do  
> class self::B # forces Ruby to wait to bind the constant  
> end  
> end  
> puts m.constants.inspect # [“B”]  
> puts m.const\_get(‘B’).inspect # #Module:0x220164::B

This does what I want, cheers
