From: Stefano Crocco Date: 2007-08-23T23:19:01+09:00 Subject: Re: What is the difference between :, ::, . ? Alle gioved� 23 agosto 2007, Xavier Noria ha scritto: > On Aug 23, 2007, at 2:56 PM, Stefano Crocco wrote: > > (remember that classes and modules are usually stored in > > constants). > > Are there exceptions? You can store a class object in any variable. For example: * in a global variable $cls = Hash $cls.new => {} * in an instance variable (the point of the example is @cls, not C, of course) class C attr_reader :cls def initialize cls @cls = cls end end c = C.new Array c.cls.new => [] * in a local variable: cls = String cls.new => '' I think that the code class MyClass end does two different things: 1) creates an instance of class Class with name MyClass; 2) stores this instance in the constant MyClass. If you call Class.new, you get an anonymous class object which is not stored in a constant. Another interesting example is Struct.new. It returns a class object corresponding to the new class, but it doesn't get stored in a constant, unless you pass a name to Struct.new or explicitly put it in a constant: cls = Struct.new :attr1, :attr2 => # #the class is only stored in the local variable cls Cls = Struct.new :attr1, :attr2 => Cls #now the class is stored in the constant S cls = Struct.new 'Cls', :attr1, :attr2 => Struct::Cls #now the class is stored in the constant Struct::Cls Stefano