From: Xavier Shay Date: 2008-10-10T01:03:37+09:00 Subject: Re: Enumerating constants in anonymous modules David A. Black wrote: > Hi -- > > On Fri, 10 Oct 2008, Xavier Shay 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 # #::B This does what I want, cheers -- Posted via http://www.ruby-forum.com/.