From: sur max Date: 2007-03-14T17:59:06+09:00 Subject: Re: Where is my Class ? ------=_Part_25164_28935791.1173862736877 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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} # # B.instance_methods.include?("foo") # true B.__id__ # 24100884 a=Class.new # a.module_eval{define_method :bar do; puts "bar here"; end} # 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 wrote: > > sur max schrieb: > > assignment happened LEFT-to-RIGHT > > > >> a = Class.new > >> # # > >> > >> 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 # .... > 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 # => # > 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: > > c1 = Class.new # => # > c2 = Class.new # => # > > C = c1 # => C > C = c2 # warning: already initialized constant C > # => C > > c1 # => C > c2 # => C > > They still are distinct classes, though. > > c1.object_id # => 25669730 > c2.object_id # => 25664850 > > Regards, > Pit > > -- sur "is a String object" is a String object http://expressica.com ------=_Part_25164_28935791.1173862736877--