[NEWBIE] Help with class instance variables

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

Hi –

On Mon, 14 Jul 2008, Ittay D. wrote:

instance and returns it)
module Platform
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

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?
  2. Is there an analogous way to ‘initialize’ to write initialization of
    class variables?

Thank you,
Ittay

David

Hi –

On Mon, 14 Jul 2008, Ittay D. wrote:

class C
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

  1. 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

Hi,

Rick Denatale wrote:

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.

Let me rephrase, trying to to use any terminology. Let’s say I have a
class Person whose instances have variables like name and age. Now, the
Person class also has a cache for all persons, loaded from a file. So
this is a single variable shared by all instances and is initialized
once. How do I initialize it in Ruby?

On Mon, Jul 14, 2008 at 7:30 AM, Ittay D. [email protected]
wrote:

class C
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

  1. Is there an analogous way to ‘initialize’ to write initialization of
    class variables?
    / http://www.ruby-forum.com/.

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/

Thank you very much!

Rick Denatale wrote:

Well, it depends on just what you are doing with the cache.
[SNIP]

On Mon, Jul 14, 2008 at 7:58 AM, Ittay D. [email protected]
wrote:

instances
once. How do I initialize it in Ruby?

Well, it depends on just what you are doing with the cache.

class Person

def self.setup_cache
@cache = #… whatever you do to initialize the cache
end

setup_cache

Now assuming we’ve got some kind of key to access the cache we

probably
want a class method to get

cached people

def self.person(cache_key)
@cache[cache_key]
end
end

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/