Hi --

On Mon, 14 Jul 2008, Ittay Dror wrote:

> Hello,
>
> The code below prints:
> #<Gem::Platform:0x..fdbdf0696 @cpu="x86", @os="linux", @version=nil>
> --
>
> 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!