From: Henry Maddocks Date: 2012-06-10T09:17:53+09:00 Subject: Re: Class variables and self --Boundary_(ID_g3Q5WKDdifaf3Nw6HV2Yxg) Content-type: text/plain; CHARSET=US-ASCII Content-transfer-encoding: 7BIT On 10/06/2012, at 12:02 PM, Jan E. wrote: > > No, they are never the same object. self.class in the context of Car is > not Car but Class, because the class of a class is Class. > > The only context where self and self.class are the same is the context > of Class, because Class is an instance of itself. Did you read what I wrote? I said, it depends where the calls are made. I assumed the OP was asking the question based on the code that he posted. The two calls were made in different parts of the code where self were different objects. 1.9.3p125 :021 > class Car 1.9.3p125 :022?> # Here self == Car 1.9.3p125 :023 > p self 1.9.3p125 :024?> 1.9.3p125 :025 > def initialize 1.9.3p125 :026?> # Here self is an instance of Car so self.class == Car 1.9.3p125 :027 > p self.class 1.9.3p125 :028?> end 1.9.3p125 :029?> 1.9.3p125 :030 > end Car => nil 1.9.3p125 :031 > 1.9.3p125 :032 > Car.new Car => # 1.9.3p125 :033 > self and self.class are both Car. Sure if they two calls were made in the same context they would return different objects but, this isn't the case in the code the OP posted. Henry --Boundary_(ID_g3Q5WKDdifaf3Nw6HV2Yxg) Content-type: text/html; CHARSET=US-ASCII Content-transfer-encoding: quoted-printable

No,= they are never the same object. self.class in the context of Car is =
not Car but Class, because the class of a class is Class.

The = only context where self and self.class are the same is the context =
of Class, because Class is an instance of = itself.

Did you read what I wrote? = I said, it depends where the calls are = made. 

I assumed the OP was asking the = question based on the code that he posted. The two calls were made in = different parts of the code where self were different = objects.

1.9.3p125 :021 > class Car
1.9.3p125 :022?> =     # Here self =3D=3D Car
1.9.3p125 :023 >   =     p self
1.9.3p125 :024?>   
1.9.3p125 = :025 >      def initialize
1.9.3p125 :026?> =       # Here self is an instance of Car so self.class =3D=3D= Car
1.9.3p125 :027 >         p = self.class
1.9.3p125 :028?>      end
1.9.3p125 = :029?>   
1.9.3p125 :030 >     = end
Car
 =3D> nil 
1.9.3p125 :031 = > 
1.9.3p125 :032 >   Car.new
Car
 =3D> = #<Car:0x007fcc1a3fca30> 
1.9.3p125 :033 = > 


self and self.class = are both Car. Sure if they two calls were made in the same context they = would return different objects but, this isn't the case in the code the = OP = posted.

Henry

= --Boundary_(ID_g3Q5WKDdifaf3Nw6HV2Yxg)--