I don't think Class.new should create a new constant. I think it would be much cleaner if it optionally took a symbol for the name of the class to create. You can then assign the result (i.e. an anonymous or named class) to whatever constant/variable you want:

Foo = Class.new(SuperClass, :Foo)

This would have a nice property that once a class is created its name couldn't be accidentally changed (whether it's anonymous or not). Should settingf the name of an anonymous class be a desired feature I see no problem todd name= method that will do so. Of course the method would throw an exception if the class is not anonymous.

BTW: It's not the first-const-assign-..., as one may think looking at simple examples. It's actually pretty random. I have no clue what's going on there. Try to copy this in a file and run it:

x = Class.new
A = x
B = x
C = x

p x.name  # prints "C"

module M1
  y = Class.new
  A = y
  B = y
  C = y

  p y.name  # prints "M1::A"
end

module M3
  z = Class.new
  U = z
  V = z
  W = z

  p z.name  # prints "M3::V"
end

module M4
  q = Class.new
  I = q
  J = q
  K = q

  p q.name  # prints "M4::K"
end

You can play with local variable names (change q to z, for example) and getifferent results. You can get yet another results when typed in irb.

Tomas

-----Original Message-----
From: Charles.O.Nutter / sun.com [mailto:Charles.O.Nutter / sun.com] On Behalf Of Charles Oliver Nutter
Sent: Tuesday, October 16, 2007 9:07 PM
To: ruby-core / ruby-lang.org
Subject: Re: A couple of bugs?

Tomas Matousek wrote:
> Well, that's interesting. Then this seems to be the only assignment that has side-effect on RHS, which I would argue nobody expects (or do you?). I think it makes more sense to do naming in class definition construct ratherhan to check whether RHS is an anonymous class on each constant assignment.
>
> Hence I consider this a bug.

It would not be directly possible for Class.new to assign a constant in
the caller's scope, since by calling Class.new you're activating a new
containing constant scope. So the behavior you're looking for would
require at least as much magic to implement.

The first-const-assign-names-anonymous-class behavior is still a little
bit magic, but it's at least a bit more explicit. Class.new creating
constants in the caller's scope would be much more peculiar in my opinion.

- Charlie