On Wed, Mar 12, 2014 at 9:40 AM, Arup Rakshit <lists / ruby-forum.com> wrote:

module M
>   class Z
>     class << self
>       X = "foo"
>   def bar; 12 ; end
>     end
>   end
> end
>
> M::Z.bar # => 12
> M::Z::X # uninitialized constant M::Z::X (NameError)
>
> When we are able to access the method `bar` but why not the constant `X`
> ?


M::Z evaluates to a class object.

That class object has a singleton method called "bar", so you can invoke
the method on the class object.

On the other hand that "X" is stored in the set of constants of the
singleton class of M::Z, let's call it S. M::Z::X looks for "X" in the
constants table of M::Z and its ancestors, since none of them is S, the
lookup fails.