The first time an anonymous class is assigned to a constant, its name
changes to be the constant's. You can check that everything works as you
expected by doing
B=a
it gives
# warning: already intialized constant B .... thats fine
but
and now if i check a as
a
# B
so where is the original class of a that was #<Class:0x2de1f00> .... and
how
come the assignment happens from RIGHT-to-LEFT ?
any idea ?
sur max, you still have the same class, accessible via both a and B:
a = Class.new # => #<Class:0x310b640>
a.object_id # => 25713440
B = Class.new # => B
B.object_id # => 25706400
B = a # warning: already initialized constant B
# => B
a # => B
a.object_id # => 25713440
B.object_id # => 25713440
The reason is that Ruby uses the name of the first constant a class object is assigned to as the class' name:
X = Class.new # => X
Y = X # => X
Y # => X
In your case, you created an "anonymous" class without assigning it to a constant, so it doesn't have a name yet. After assigning this anonymous class to the constant B, you get the warning, but nethertheless the constant is changed to reference the previous anonymous class. After the assignment, the class isn't anonymous anymore, it has got the name "B".
This way you can create multiple classes with the same name:
B=a # warning: already initialized constant B
=> B
a.instance_methods.include?("bar") # true
a.instance_methods.include?("foo") # false
B.instance_methods.include?("foo") # false
B.instance_methods.include?("bar") # true
B.__id__ # 24067260
B.__id__==a.__id__ # true
this means that the Class B is overwritten by the Class referenced by "a"
so it is the case of changing a constant. Is that possible ?
···
On 3/14/07, Pit Capitain <pit@capitain.de> wrote:
sur max schrieb:
> assignment happened LEFT-to-RIGHT
>
>> a = Class.new
>> # #<Class:0x2de1f00>
>>
>> B = Class.new
>> # B
>>
>> B=a
>> it gives
>> # warning: already intialized constant B .... thats fine
>>
>> but
>> and now if i check a as
>>
>> a
>> # B
>>
>> so where is the original class of a that was #<Class:0x2de1f00> ....
and
>> how
>> come the assignment happens from RIGHT-to-LEFT ?
>>
>> any idea ?
sur max, you still have the same class, accessible via both a and B:
a = Class.new # => #<Class:0x310b640>
a.object_id # => 25713440
B = Class.new # => B
B.object_id # => 25706400
B = a # warning: already initialized constant B
# => B
a # => B
a.object_id # => 25713440
B.object_id # => 25713440
The reason is that Ruby uses the name of the first constant a class
object is assigned to as the class' name:
X = Class.new # => X
Y = X # => X
Y # => X
In your case, you created an "anonymous" class without assigning it to a
constant, so it doesn't have a name yet. After assigning this anonymous
class to the constant B, you get the warning, but nethertheless the
constant is changed to reference the previous anonymous class. After the
assignment, the class isn't anonymous anymore, it has got the name "B".
This way you can create multiple classes with the same name:
(...)
B=a # warning: already initialized constant B
(...)
this means that the Class B is overwritten by the Class referenced by "a"
so it is the case of changing a constant. Is that possible ?
No, no objects are overwritten in Ruby. An assignment like
var = obj
just makes the object on the right (obj) accessible via the name on the left (var). The line
B = a
makes the object referenced by a (the anonymous class) accessible via the name B. After this, a and B are different names for the same object. The previous object referenced by the name B is still around (until the garbage collector reclaims it) and not changed at all.
In Ruby, constants, instance, and local variables are only names for objects. Constants have the additional property that they are intended to be assigned to only once. If you re-assign a constant, you get a warning, but the assignment happens nonetheless. Try this with something different than anonymous classes and it might be easier to understand.
There have been many discussions about variable names and objects on this mailing list. You can also look at
sur max schrieb:
> (...)
> B=a # warning: already initialized constant B
> (...)
> this means that the Class B is overwritten by the Class referenced by
"a"
> so it is the case of changing a constant. Is that possible ?
No, no objects are overwritten in Ruby. An assignment like
var = obj
just makes the object on the right (obj) accessible via the name on the
left (var). The line
B = a
makes the object referenced by a (the anonymous class) accessible via
the name B. After this, a and B are different names for the same object.
The previous object referenced by the name B is still around (until the
garbage collector reclaims it) and not changed at all.
In Ruby, constants, instance, and local variables are only names for
objects. Constants have the additional property that they are intended
to be assigned to only once. If you re-assign a constant, you get a
warning, but the assignment happens nonetheless. Try this with something
different than anonymous classes and it might be easier to understand.
There have been many discussions about variable names and objects on
this mailing list. You can also look at