# Where is my Class?

**URL:** <https://rubytalk.org/t/where-is-my-class/36145>\
**Category:** ruby-talk\
**Created:** [14 March 2007 07:12 UTC](https://rubytalk.org/t/where-is-my-class/36145 "2007-03-14T07:12:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sur](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Sur](https://rubytalk.org/u/Sur)\
**Post date:** [14 March 2007 07:12 UTC](https://rubytalk.org/t/where-is-my-class/36145/1 "2007-03-14T07:12:33Z")

</div>

Hi all,

There isn't any way to have a class name starting with lowercase letter.

I was trying it as

class a  
end  
# SyntaxError: compile error  
# class/module name must be CONSTANT

so then i tried

a = Class.new  
# #\<Class:0x2de1f00\>

this shows that "a" is now a reference to the class #\<Class:0x2de1f00\>  
the Class object

and  
B = Class.new  
# B

that works absolutely fine.

The point is if we try to assign as

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  
> "is a String object" is a String object  
> [http://expressica.com](http://expressica.com)

---

<div class="post-metadata">

**Author:** ![Sur](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Sur](https://rubytalk.org/u/Sur)\
**Post date:** [14 March 2007 07:17 UTC](https://rubytalk.org/t/where-is-my-class/36145/2 "2007-03-14T07:17:51Z")

</div>

assignment happened LEFT-to-RIGHT

> **···**
>
> On 3/14/07, sur max \<sur.max@gmail.com\> wrote:
> 
> > Hi all,
> > 
> > There isn't any way to have a class name starting with lowercase letter.
> > 
> > I was trying it as
> > 
> > class a  
> > end  
> > # SyntaxError: compile error  
> > # class/module name must be CONSTANT
> > 
> > so then i tried
> > 
> > a = Class.new  
> > # #\<Class:0x2de1f00\>
> > 
> > this shows that "a" is now a reference to the class #\<Class:0x2de1f00\>  
> > the Class object
> > 
> > and  
> > B = Class.new  
> > # B
> > 
> > that works absolutely fine.
> > 
> > The point is if we try to assign as
> > 
> > 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  
> > "is a String object" is a String object  
> > [http://expressica.com](http://expressica.com)
> 
> --  
> sur  
> "is a String object" is a String object  
> [http://expressica.com](http://expressica.com)

---

<div class="post-metadata">

**Author:** ![Sylvain\_Joyeux](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Sylvain\_Joyeux](https://rubytalk.org/u/Sylvain_Joyeux)\
**Post date:** [14 March 2007 08:52 UTC](https://rubytalk.org/t/where-is-my-class/36145/3 "2007-03-14T08:52:25Z")

</div>

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

puts a.object\_id  
puts b.object\_id

> **···**
>
> --  
> Sylvain Joyeux

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [14 March 2007 07:52 UTC](https://rubytalk.org/t/where-is-my-class/36145/4 "2007-03-14T07:52:49Z")

</div>

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:

&nbsp;&nbsp;&nbsp;a = Class.new # =\> #\<Class:0x310b640\>  
&nbsp;&nbsp;&nbsp;a.object\_id # =\> 25713440

&nbsp;&nbsp;&nbsp;B = Class.new # =\> B  
&nbsp;&nbsp;&nbsp;B.object\_id # =\> 25706400

&nbsp;&nbsp;&nbsp;B = a # warning: already initialized constant B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# =\> B  
&nbsp;&nbsp;&nbsp;a # =\> B  
&nbsp;&nbsp;&nbsp;a.object\_id # =\> 25713440  
&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;X = Class.new # =\> X  
&nbsp;&nbsp;&nbsp;Y = X # =\> X  
&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;c1 = Class.new # =\> #\<Class:0x30f60c4\>  
&nbsp;&nbsp;&nbsp;c2 = Class.new # =\> #\<Class:0x30f3aa4\>

&nbsp;&nbsp;&nbsp;C = c1 # =\> C  
&nbsp;&nbsp;&nbsp;C = c2 # warning: already initialized constant C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# =\> C

&nbsp;&nbsp;&nbsp;c1 # =\> C  
&nbsp;&nbsp;&nbsp;c2 # =\> C

They still are distinct classes, though.

&nbsp;&nbsp;&nbsp;c1.object\_id # =\> 25669730  
&nbsp;&nbsp;&nbsp;c2.object\_id # =\> 25664850

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Sur](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Sur](https://rubytalk.org/u/Sur)\
**Post date:** [14 March 2007 08:59 UTC](https://rubytalk.org/t/where-is-my-class/36145/5 "2007-03-14T08:59:06Z")

</div>

Hi Pit,

from your explanation and my perception too it seems that we are not able to  
write anything in the constants once they defined.  
Look at this then....

B=Class.new # B

B.module\_eval{define\_method :foo do; puts "foo here"; end} #  
#\<Proc:0x02deeb18@(irb):3\>

B.instance\_methods.include?("foo") # true

B.\_\_id\_\_ # 24100884

a=Class.new # \<Class:0x2de7978\>

a.module\_eval{define\_method :bar do; puts "bar here"; end}  
#\<Proc:0x02de2920@(irb):7\>

a.\_\_id\_\_ # 24067260

a.instance\_methods.include?("foo") # false

a.instance\_methods.include?("bar") # true

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:
> > 
> > &nbsp;&nbsp;&nbsp;a = Class.new # =\> #\<Class:0x310b640\>  
> > &nbsp;&nbsp;&nbsp;a.object\_id # =\> 25713440
> > 
> > &nbsp;&nbsp;&nbsp;B = Class.new # =\> B  
> > &nbsp;&nbsp;&nbsp;B.object\_id # =\> 25706400
> > 
> > &nbsp;&nbsp;&nbsp;B = a # warning: already initialized constant B  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# =\> B  
> > &nbsp;&nbsp;&nbsp;a # =\> B  
> > &nbsp;&nbsp;&nbsp;a.object\_id # =\> 25713440  
> > &nbsp;&nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;&nbsp;X = Class.new # =\> X  
> > &nbsp;&nbsp;&nbsp;Y = X # =\> X  
> > &nbsp;&nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;&nbsp;c1 = Class.new # =\> #\<Class:0x30f60c4\>  
> > &nbsp;&nbsp;&nbsp;c2 = Class.new # =\> #\<Class:0x30f3aa4\>
> > 
> > &nbsp;&nbsp;&nbsp;C = c1 # =\> C  
> > &nbsp;&nbsp;&nbsp;C = c2 # warning: already initialized constant C  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# =\> C
> > 
> > &nbsp;&nbsp;&nbsp;c1 # =\> C  
> > &nbsp;&nbsp;&nbsp;c2 # =\> C
> > 
> > They still are distinct classes, though.
> > 
> > &nbsp;&nbsp;&nbsp;c1.object\_id # =\> 25669730  
> > &nbsp;&nbsp;&nbsp;c2.object\_id # =\> 25664850
> > 
> > Regards,  
> > Pit
> 
> --  
> sur  
> "is a String object" is a String object  
> [http://expressica.com](http://expressica.com)

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [14 March 2007 09:42 UTC](https://rubytalk.org/t/where-is-my-class/36145/6 "2007-03-14T09:42:56Z")

</div>

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

&nbsp;&nbsp;&nbsp;var = obj

just makes the object on the right (obj) accessible via the name on the left (var). The line

&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;[http://onestepback.org/index.cgi/Tech/Ruby/Shoeboxes.rdoc](http://onestepback.org/index.cgi/Tech/Ruby/Shoeboxes.rdoc)

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Sur](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Sur](https://rubytalk.org/u/Sur)\
**Post date:** [14 March 2007 19:05 UTC](https://rubytalk.org/t/where-is-my-class/36145/7 "2007-03-14T19:05:54Z")

</div>

Hi Pit,  
Thanks a lot for explaining.

> **···**
>
> On 3/14/07, Pit Capitain \<pit@capitain.de\> wrote:
> 
> > 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
> > 
> > &nbsp;&nbsp;&nbsp;var = obj
> > 
> > just makes the object on the right (obj) accessible via the name on the  
> > left (var). The line
> > 
> > &nbsp;&nbsp;&nbsp;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
> > 
> > &nbsp;&nbsp;&nbsp;[http://onestepback.org/index.cgi/Tech/Ruby/Shoeboxes.rdoc](http://onestepback.org/index.cgi/Tech/Ruby/Shoeboxes.rdoc)
> > 
> > Regards,  
> > Pit
> 
> --  
> sur  
> "is a String object" is a String object  
> [http://expressica.com](http://expressica.com)
