[NEWBIE] Help with class instance variables

I

Ittay Dror

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 :eek:s
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
 
D

David A. Black

Hi --

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 :eek:s
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
 
I

Ittay Dror

Hi,
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
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

Hi,


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?
/ <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.
 
D

David A. Black

Hi --

Hi,


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
 
I

Ittay Dror

Hi,

Rick said:
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?
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

Hi,



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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top