Kero van Gelder <kero / d4050.upc-d.chello.nl> writes:

> >  What do you want to do with such a function ?
> 
> Construct a class with a name provided by the user.  It would be
> derived from another class.  (OK, that was a couple of weeks ago, I
> decided to implement it differently by now.)

I'm not going to explain this well, so bear with me.

Classes don't really have first class names in Ruby. A class is an
object, just like any other:

  class Fred
    def say_hello
       puts "hi"
    end
  end

  xxx = Fred

  obj = xxx.new
  obj.say_hello

When you run a class definition, Ruby creates a constant in that scope
with the name that you gave on the class definition, and sticks a
reference o the class object into that constant. That's why you can
say Fred.new. It isn't that the compiler is looking for a class called
Fred at this point: it's just sending the message 'new' to the
constant Fred, which happens to contain a class object.

OK, now the tricky part. You say: but wait, Dave. I can type
obj.class, and Ruby will tell me it's an object of class Fred. How's
it know that, huh, unless classes have names.

Well, it cheats. The string class names are associated with classes
when they're first attached to a constant.

  a = Class.new    # create an anonymous new class
  p a.name         # ""
  Wombat = a
  p a.name         # "Wombat"

Note that the name has nothing to do with the behavior: it's simply a
tag.

So, to summarize, in Ruby, classes are first class objects, and do not
have intrinsic names. However, for convenience, they are assigned an
external name when they are associated with a constant.


Regards


Dave