From: "David A. Black" Date: 2008-07-14T20:16:52+09:00 Subject: Re: [NEWBIE] Help with class instance variables Hi -- On Mon, 14 Jul 2008, Ittay Dror wrote: > Hello, > > The code below prints: > # > -- > > that is, the @arch, @os and @classifier are empty. > > (if i set these variables in the id method, they work fine) > (Gem::Platform.local is a class method that creates a new Gem::Platform > instance and returns it) > > thank you for your help, > ittay > > code below: > > require 'rubygems' > require 'rubygems/version' > require 'rubygems/platform' > > module Platform > class << self > attr_reader :arch > attr_reader :os > attr_reader :classifier > @arch = Gem::Platform.local.cpu > @os = Gem::Platform.local.os > @classifier = Gem::Platform.local.version > > def id > "#{@arch}-#{@os}-#{@classifier}" > end > end > end > > puts Gem::Platform.local.inspect > puts Platform.id 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 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!