# \[NEWBIE\] Help with class instance variables

**URL:** <https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007>\
**Category:** Ruby\
**Created:** [July 14, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007 "2008-07-14T13:09:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/1 "2008-07-14T13:09:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/2 "2008-07-14T13:22:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 1:34pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/3 "2008-07-14T13:34:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/4 "2008-07-14T13:48:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/5 "2008-07-14T14:02:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/6 "2008-07-14T13:47:51Z")

</div>

On Mon, Jul 14, 2008 at 7:30 AM, Ittay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](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/](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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/7 "2008-07-14T16:57:58Z")

</div>

Thank you very much!

Rick Denatale wrote:

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

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [July 14, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/newbie-help-with-class-instance-variables/143007/8 "2008-07-14T16:52:22Z")

</div>

On Mon, Jul 14, 2008 at 7:58 AM, Ittay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://talklikeaduck.denhaven2.com/)
