From: "David A. Black" Date: 2008-07-14T20:43:30+09:00 Subject: Re: Help with class instance variables Hi -- On Mon, 14 Jul 2008, 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 > 2. Is there an analogous way to 'initialize' to write initialization of > class variables? Change @x to @@x. Keep in mind, though, that in spite of the similar appearance, class variables are completely different from instance variables in just about every possible respect. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates!