On Sat, Mar 24, 2007 at 03:59:39AM +0900, John Lam (CLR) wrote:
> From: "John Lam (CLR)" <jflam / microsoft.com>
> To: "ruby-core / ruby-lang.org" <ruby-core / ruby-lang.org>
> Subject: Scope of class variables vs. constants
> Date: Sat, 24 Mar 2007 03:59:39 +0900
> 
> Dumb question of the day:
> 
> Is the scope of class variables and constants exactly the same in all cases? IOW, are they stored in two different dictionaries on the same class/module?

Only when the class variable is a constant?

Its just a convention that class objects are assigned to contants, though
partially enforced through the class creation syntax:

irb(main):001:0> class foo; end
SyntaxError: compile error
(irb):1: class/module name must be CONSTANT
class foo; end
          ^
        from (irb):1
        from :0
irb(main):002:0> foo = Object
=> Object
irb(main):003:0> foo.new
=> #<Object:0xb7a01bb4>
irb(main):004:0> Const = Object
=> Object
irb(main):005:0> Const.new
=> #<Object:0xb79fe7c0>


So the scope of contants is always the same, whether the value is a class or
something else, but classes don't have to be accessed by referencing the
contant, they can also be referenced as values.

Sam