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