From: Christoph Date: 2003-01-04T04:54:24+09:00 Subject: Re: toplevel modules and classes "Eugene Scripnik" wrote .... > Well, I just wanted to build classes/modules tree of my application. > This structure is so complicated. And I wanted to do it in generalized > way and also was surprised when I put another class/module into class > and compiler found it OK. > So, both module and class accept defining modules and classes inside > them, but Class#constants and Module#constants behave in different > way. That was second surprise. There is instance_method and a singleton_method (class_method) of the class "Module" called `constant' . --- class Module p singleton_methods.include?('constants') # true end class Module p instance_methods.include?('constants') # true end --- The singleton version calculates the current visible constants - this depends on the current binding - here is an old example illustrating the difference between Object.constants (calling the instance version) and Module.constants (Class.instance would work too) calling the singleton version. --- def minus (bind) puts eval('Module.constants - Object.constants',bind).sort.join(', ') end A0 = 1 module A B = 'B' class C D = 'D' end class E F = 'F' end end minus binding # => module A minus binding # => B, C, E class C minus binding # => B, C, D, E end class E minus binding # => B, C, D, F end end --- /Christoph