From: Dave Thomas Date: 2001-06-27T08:04:02+09:00 Subject: [ruby-talk:16939] Re: rb_define_class() vs Class.new() Kero van Gelder writes: > The name of a class is _the_ name of that class. > I do not understand what you mean with ``just a name''. Classes do not need to have names. It's a pure convenience used when inspecting objects of that class, and when using things such as Class#name. When you say class Kero # ... end it is not an essential part of Ruby's semantics that the class you create is named 'Kero'. The class could equally well not have a name. All that happens is that a constant is created, called Kero, and that constant is set to point to the class object for the class you just created. 'Kero' is _not_ the name of the class. 'Kero' is the name of the constant that references the class. When you say k = Kero.new you are not using a class named Kero. Instead, you're simply sending the message 'new' to the object referenced by the constant Kero. As that constant references a class object, the statement does what you'd expect. fred = Kero k = fred.new Here we're assigned to a variable 'fred' whatever Kero referenced. As that was a class, we can use fred.new to create new instances. Creating anonymous classes is basically the same Dave = Class.new Here Dave is a constant that points to an anonymous class. fred = Class.new And here 'fred' points to a different class. Now... to the naming issue. Ruby classes do not need names: they are only there for output purposes. So, matz decided to implement names by cheating: when you ask for a class name, Ruby searches through the constants until it finds one that references the class, and then uses it. For efficiency, it caches the results of doing this in the class object. For anonymous objects, the lookup and caching is done when you first try to ask for the name. For classes defined using 'class XXX', the caching is done when the class is defined. Regards Dave