From: Rick DeNatale Date: 2008-07-14T20:42:24+09:00 Subject: Re: Help with class instance variables ------=_Part_52921_30299140.1216036014276 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Mon, Jul 14, 2008 at 7:30 AM, Ittay Dror wrote: > Hi, > > David A. Black wrote: > > Hi -- > > > > > > The problem is that the instance variables you're initializing belong > > to the singleton class of Platform (class << Platform), not Platform. > > Compare with this: > > > > class C > > class << self > > attr_accessor :x > > end > > > > @x = 1 # or self.x = 1 > > end > > > > puts C.x # 1 > > > > Ok, thank you, that cleared things. I have two follow up questions: > 1. If I want to add an instance variable to an object of class C I can > only do that in a method (not as part of the class definition)? In the > example above, how can I change it so that C.new will return an instance > of C where '@x' of the instance is 1? class C def initialize @x = 1 end end Perhaps this might help. http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway > > 2. Is there an analogous way to 'initialize' to write initialization of > class variables? > / . > You haven't used an class variables on this thread. Your initial code created class instance variables, i.e. instance variables belonging to the object which represents the class. Class variables in Ruby use a double @ prefix, are visible/shared between the class, any subclasses, and instances of the same, have some interesting quirks in their semantics and are avoided or at least used sparingly by many experienced Rubyists. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_52921_30299140.1216036014276--