# Class (not instance) Initializer

**URL:** <https://www.ruby-forum.com/t/class-not-instance-initializer/140218>\
**Category:** Ruby\
**Created:** [June 12, 2008, 12:30am UTC](https://www.ruby-forum.com/t/class-not-instance-initializer/140218 "2008-06-12T00:30:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2008, 12:30am UTC](https://www.ruby-forum.com/t/class-not-instance-initializer/140218/1 "2008-06-12T00:30:33Z")

</div>

I would like to populate a class variable using a method

class SomeThing  
@@some\_data  
populate\_some\_data

def populate\_some\_data  
end  
end

I expected that prefixing the method with self would work. It doesn’t.

…  
self.populate\_some\_data

def self.populate\_some\_data  
end  
…

Poking around in a couple books I don’t see anything that addresses  
this. What’s the correct syntax for this? Or, if there is no way to run  
a method, what’s the best way to pull a trigger to have @@some\_data  
populated the first time any object of SomeThings is instantiated?

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2008, 12:43am UTC](https://www.ruby-forum.com/t/class-not-instance-initializer/140218/2 "2008-06-12T00:43:29Z")

</div>

Greg W. wrote:

> this. What’s the correct syntax for this? Or, if there is no way to run  
> a method, what’s the best way to pull a trigger to have @@some\_data  
> populated the first time any object of SomeThings is instantiated?
> 
> – gw

Well, if you want to wait to do the initialization until the first  
object is created, then something like this might work:

def initialize  
populate\_some\_data if @@some\_data == nil  
…  
end

But if all you want to do it initialize @@some\_data, just put the  
initialization code in the class definition:

class SomeThing  
@@some\_data = [1,2,3]  
def initialize  
…  
end  
end

Ruby will execute the code as it creates the class. Try this:

~$ irb  
irb(main):001:0\> class SomeThing  
irb(main):002:1\> puts “Hiya!”  
irb(main):003:1\> end  
Hiya!  
=\> nil

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2008, 12:51am UTC](https://www.ruby-forum.com/t/class-not-instance-initializer/140218/3 "2008-06-12T00:51:05Z")

</div>

Tim H. wrote:

> Well, if you want to wait to do the initialization until the first  
> object is created, then something like this might work:
> 
> def initialize  
> populate\_some\_data if @@some\_data == nil  
> …  
> end

Duh. I should have figured that one out. Thanks.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2008, 2:46am UTC](https://www.ruby-forum.com/t/class-not-instance-initializer/140218/4 "2008-06-12T02:46:04Z")

</div>

> I expected that prefixing the method with self would work. It doesn’t.
> 
> …  
> self.populate\_some\_data
> 
> def self.populate\_some\_data  
> end  
> …

Your issue here is that code within a class is executed a line at a  
time, so populate\_some\_data is called before it is defined. Also, you  
don’t need to use self when calling populate\_some\_data, as the method is  
already called within the scope of the class (though this is just a  
style issue). So the following code will work:

…  
def self.populate\_some\_data  
end  
populate\_some\_data  
…

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 12, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/class-not-instance-initializer/140218/5 "2008-06-12T13:43:38Z")

</div>

On Thu, Jun 12, 2008 at 12:50 AM, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> – gw
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

It is most unfortunate that class variables are still advocated so much  
:(.

The last but one line shows one of the problems of class variables,  
they violate the principle that a base class shall never be touched by  
the implementation of its subclasses - I know there is a classy name  
for this but I forgot it 😉

--------------------------- 8\< ---------------------------  
203/79 \> cat class\_vars.rb && ruby class\_vars.rb

# vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu syn=on:

# file: class\_vars.rb

class Base  
@@cvar=:Base  
@cinstvar=:base  
class \<\< self  
attr\_reader :cinstvar  
def cvar; @@cvar end  
end  
end

class Sub \< Base  
@@cvar = :Sub  
@cinstvar = :sub  
end

puts “@@cvar in Sub: #{Sub.cvar}”  
puts “@cinstaver in Sub: #{Sub.cinstvar}”  
puts "===“_20  
puts "@@cvar in Base: **#{Base.cvar}** _”  
puts “@cinstaver in Base: #{Base.cinstvar}”

# @@cvar in Sub: Sub @cinstaver in Sub: sub

@@cvar in Base: _ **Sub** _\*  
@cinstaver in Base: base

--------------------------- 8\< ------------------------------------

So the basic advice is to use Class instance variables, in your case  
that might be written as

class Something  
@some\_data = [1,2,3]  
class \<\< self; attr\_reader :some\_data end  
end

you can of course access them directly in class methods either defined  
with the self idiom

def self.print\_data; puts @some\_data end

or via the singleton class idiom

class \<\< self  
def print\_data; puts @some\_data end  
end

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

As simple as possible, but not simpler.  
Albert Einstein
