Alle gioved=EC 23 agosto 2007, Xavier Noria ha scritto:
You can store a class object in any variable. For example:
Oh yes, classes are objects.
What I actually wondered (but wording was bad) was whether you can =20
have a named class without assigning to a constant. This was a slip =20
while reading because the post mentioned "storage", which is more =20
generic and correct of course.
If I understand correctly the way this works non-anonynous classes =20
are just class objects assigned to regular constants. That is
class C
...
end
is approximately
C =3D Class.new
C.class_eval do
...
end
so to speak (I don't claim it is equivalent because I've not =20
analized all the implications). I came accross this when I tried to =20
understand Rails class reloading: you remove the constants and that =20
triggers const_missing again, which assigns a new definition to that =20
constant. The code in the interpreter looks up the constant, if it =20
exists the class is reopened, if it doesn't the class is defined.
I think that relationship between classes and constants is beautiful, =20=
they are formally decoupled (of course with some coupling in practice =20=
for convenience), and is yet another example of the consequences of =20
chosing a few core concepts and build on top of them.
-- fxn