# Class Initialization?

**URL:** <https://www.ruby-forum.com/t/class-initialization/201339>\
**Category:** Ruby\
**Created:** [January 5, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/class-initialization/201339 "2011-01-05T15:32:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 5, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/1 "2011-01-05T15:32:58Z")

</div>

I have a class and two class methods: self.encode and self.decode. The  
encode method expects a class variable (@@table) to be “initialized”.  
Whereas it’s possible to do that in def self.encode(bytes) method of my  
class, I was interested to know if I can do that elsewhere (static  
blocks, a la Java?). What is the recommended way to do such class  
initialization tasks?

-Kedar

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 5, 2011, 3:41pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/2 "2011-01-05T15:41:02Z")

</div>

On Wed, Jan 5, 2011 at 3:33 PM, Kedar M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a class and two class methods: self.encode and self.decode. The  
> encode method expects a class variable (@@table) to be “initialized”.  
> Whereas it’s possible to do that in def self.encode(bytes) method of my  
> class, I was interested to know if I can do that elsewhere (static  
> blocks, a la Java?). What is the recommended way to do such class  
> initialization tasks?

The class body is essentially what a static block is in a Java class:  
it is executed at “class load time” which in Ruby is “class definition  
time”.

15:37:59 $ ruby19 \<\<CODE

> class Foo  
> @bar = “initialized”  
> def self.ok?() @bar end  
> end  
> p Foo.ok?  
> CODE  
> “initialized”  
> 15:38:34 $

Note, please don’t use @@class\_variables - they are just too awkward.  
Using ordinary class members is totally sufficient.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 5, 2011, 3:42pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/3 "2011-01-05T15:42:18Z")

</div>

On Wed, Jan 5, 2011 at 9:33 AM, Kedar M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 

If I’m understanding right, something like one of these should work:

class MyClass  
@@table = 42;

def self.encode(bytes)  
//…  
end

def self.decode  
//…  
end  
end

OR

class MyClass  
def self.initialize\_table  
@@table = …  
end

initialize\_table

def self.encode(bytes)  
//…  
end

def self.decode  
//…  
end  
end

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 5, 2011, 6:18pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/4 "2011-01-05T18:18:15Z")

</div>

Robert, Andrew,

Thank you.  
It was not clear if the “class body” will be executed at most once.  
That’s now clear.

I like this better than Java’s static initializer!

Robert – but isn’t @@x a class variable and @x an instance variable?  
Maybe I am confused a bit. I want this “table” I am initializing to be  
available from the class, without having to create any instances of it.

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 5, 2011, 6:33pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/5 "2011-01-05T18:33:13Z")

</div>

On Wed, Jan 5, 2011 at 12:18 PM, Kedar M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> available from the class, without having to create any instances of it.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

To answer this, grasshopper, I suggest you study this code, and figure  
out  
how it works. Hint: there are more than one @count variables.

class Counter  
@count = 0

class \<\< self  
attr\_accessor :count  
end

def initialize  
self.class.count += 1  
@count = self.class.count  
end

def output  
puts “I am counter object number #{@count} of #{self.class.count}”  
end  
end

x = Counter.new  
x.output # I am counter object number 1 of 1

y = Counter.new  
x.output # I am counter object number 1 of 2  
y.output # I am counter object number 2 of 2

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 9:11am UTC](https://www.ruby-forum.com/t/class-initialization/201339/6 "2011-01-06T09:11:24Z")

</div>

On Wed, Jan 5, 2011 at 6:18 PM, Kedar M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It was not clear if the “class body” will be executed at most once.  
> That’s now clear.

Good!

> I like this better than Java’s static initializer!
> 
> Robert – but isn’t @@x a class variable and @x an instance variable?  
> Maybe I am confused a bit. I want this “table” I am initializing to be  
> available from the class, without having to create any instances of it.

Short answer: classes can have instance variables as well. Long  
answer (please see Andrew’s reply). Variables with @@ are really a  
hybrid which is directly readable from the class and instance. But  
there are some subtle issues with order in which they are created and  
so I always recommend to not use them. You’ll find plenty of  
discussions of this in the archives.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 4:48pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/7 "2011-01-06T16:48:21Z")

</div>

Kedar M. wrote in post #972556:

> It was not clear if the “class body” will be executed at most once.  
> That’s now clear.

And indeed, the execution of the code is what defines the class. The  
class and methods are defined _at runtime_.

Foo.bar rescue nil # uninitialized constant Foo  
class Foo  
def self.bar  
puts “hello!”  
end  
end  
Foo.bar # this works

Amongst other things, this means you can conditionally define classes  
and methods.

However the code will be executed multiple times if you force it to be -  
for example using ‘load’ instead of ‘require’. This can also happen if  
you require the same file using variations of the name, e.g.

require ‘foo’  
require ‘./foo’  
require ‘/path/to/foo’

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 8:41pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/8 "2011-01-06T20:41:47Z")

</div>

Andrew W. wrote in post #972567:

> On Wed, Jan 5, 2011 at 12:18 PM, Kedar M.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > available from the class, without having to create any instances of it.
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> To answer this, grasshopper, I suggest you study this code, and figure  
> out  
> how it works. Hint: there are more than one @count variables.
> 
> class Counter  
> @count = 0
> 
> class \<\< self  
> attr\_accessor :count  
> end
> 
> def initialize  
> self.class.count += 1  
> @count = self.class.count  
> end
> 
> def output  
> puts “I am counter object number #{@count} of #{self.class.count}”  
> end  
> end
> 
> x = Counter.new  
> x.output # I am counter object number 1 of 1
> 
> y = Counter.new  
> x.output # I am counter object number 1 of 2  
> y.output # I am counter object number 2 of 2

My n00b parser is stuck at class \<\< self for I have to yet learn that  
construct, I guess.

But I will spend more time understanding this example. My first attempt  
tells me that there are two types of variables (one: class, other  
instance) and both are named count.

I am still influenced by the “initially straightforward” example given  
in the Pickaxe book that counts the number of Song objects created thus  
far (i.e. incrementing @@count by 1 in the initialize method).

Thank you.

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 8:45pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/9 "2011-01-06T20:45:39Z")

</div>

Brian C. wrote in post #972835:

> Kedar M. wrote in post #972556:
> 
> > It was not clear if the “class body” will be executed at most once.  
> > That’s now clear.
> 
> And indeed, the execution of the code is what defines the class. The  
> class and methods are defined _at runtime_.
> 
> Foo.bar rescue nil # uninitialized constant Foo  
> class Foo  
> def self.bar  
> puts “hello!”  
> end  
> end  
> Foo.bar # this works
> 
> Amongst other things, this means you can conditionally define classes  
> and methods.
> 
> However the code will be executed multiple times if you force it to be -  
> for example using ‘load’ instead of ‘require’. This can also happen if  
> you require the same file using variations of the name, e.g.
> 
> require ‘foo’  
> require ‘./foo’  
> require ‘/path/to/foo’

Brian,

Interesting. So, the initialization is that of a file and not a class? I  
thought no matter how some ruby code “requires” a class defined  
elsewhere, the class initialization will be done at most once.

Thanks for the hint.

-Kedar

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 8:57pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/10 "2011-01-06T20:57:35Z")

</div>

On Thu, Jan 6, 2011 at 2:41 PM, Kedar M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > To answer this, grasshopper, I suggest you study this code, and figure  
> > def initialize  
> > x.output # I am counter object number 1 of 1  
> > instance) and both are named count.

Well, there are two variables, on two different objects (the class and  
its  
instance), but the point is that they’re _not_ two different types. They  
work the same way, they’re just on two different objects.

Here’s a snipped that avoids the mean “class \<\<” stuff (with my  
apologies  
for my laziness):

class Counter  
@count = 0

def self.count  
@count  
end

def self.count=(val)  
@count = val  
end

def initialize  
self.class.count += 1  
@count = self.class.count  
end

def output  
puts “I am counter object number #{@count} of #{self.class.count}”  
end  
end

x = Counter.new  
x.output # I am counter object number 1 of 1

y = Counter.new  
x.output # I am counter object number 1 of 2  
y.output # I am counter object number 2 of 2

I am still influenced by the “initially straightforward” example given

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/11 "2011-01-06T20:43:04Z")

</div>

Robert K. wrote in post #972742:

> On Wed, Jan 5, 2011 at 6:18 PM, Kedar M.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > It was not clear if the “class body” will be executed at most once.  
> > That’s now clear.
> 
> Good!
> 
> > I like this better than Java’s static initializer!
> > 
> > Robert – but isn’t @@x a class variable and @x an instance variable?  
> > Maybe I am confused a bit. I want this “table” I am initializing to be  
> > available from the class, without having to create any instances of it.
> 
> Short answer: classes can have instance variables as well. Long  
> answer (please see Andrew’s reply). Variables with @@ are really a  
> hybrid which is directly readable from the class and instance. But  
> there are some subtle issues with order in which they are created and  
> so I always recommend to not use them. You’ll find plenty of  
> discussions of this in the archives.

Perhaps these are the Ruby Pro techniques. I think I have to read more  
and experiment more to understand them better.

Thank you.

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 10:44pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/12 "2011-01-06T22:44:17Z")

</div>

On Jan 6, 2011, at 3:06 PM, Kedar M. wrote:

> And in the process of explaining this, you also indirectly explained one  
> question that was lurking in my mind – are the  
> attr\_readers/writers/accessors available to class variables – and as I  
> understand it, they are – by way of your previous class \<\< self  
> construct.

Careful here. The attr\* methods are manipulating instance variables and  
not class variables.

instance variables: private to an object  
class instance variables: instance variables for an object that  
just happens to be a class  
class variables: nasty, confusing things that have the same name  
but not at all the same semantics as ‘class  
variables’ in other languages

I think it is very unfortunate that a lot of introductory Ruby texts  
choose  
to emphasize class variables (@@foo) over plain old instance variables  
of  
a class (@foo for a class object).

If there were one feature I would remove from Ruby (maybe in Ruby 2.0?)  
it  
would be class variables.

Gary W.

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 7, 2011, 9:27am UTC](https://www.ruby-forum.com/t/class-initialization/201339/13 "2011-01-07T09:27:29Z")

</div>

On Thu, Jan 6, 2011 at 10:43 PM, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> not class variables.  
> to emphasize class variables (@@foo) over plain old instance variables of  
> a class (@foo for a class object).

Completely agree.

> If there were one feature I would remove from Ruby (maybe in Ruby 2.0?) it  
> would be class variables.

+2

We could start by flagging usage of class variables with a warning -  
at least with option -w set.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kedarmhaswade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@kedarmhaswade](https://www.ruby-forum.com/u/kedarmhaswade)\
**Post date:** [January 6, 2011, 9:06pm UTC](https://www.ruby-forum.com/t/class-initialization/201339/14 "2011-01-06T21:06:23Z")

</div>

Andrew W. wrote in post #972912:

> On Thu, Jan 6, 2011 at 2:41 PM, Kedar M.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > > To answer this, grasshopper, I suggest you study this code, and figure  
> > > def initialize  
> > > x.output # I am counter object number 1 of 1  
> > > instance) and both are named count.
> 
> Well, there are two variables, on two different objects (the class and  
> its  
> instance), but the point is that they’re _not_ two different types. They  
> work the same way, they’re just on two different objects.
> 
> Here’s a snipped that avoids the mean “class \<\<” stuff (with my  
> apologies  
> for my laziness):
> 
> class Counter  
> @count = 0
> 
> def self.count  
> @count  
> end
> 
> def self.count=(val)  
> @count = val  
> end
> 
> def initialize  
> self.class.count += 1  
> @count = self.class.count  
> end
> 
> def output  
> puts “I am counter object number #{@count} of #{self.class.count}”  
> end  
> end
> 
> x = Counter.new  
> x.output # I am counter object number 1 of 1
> 
> y = Counter.new  
> x.output # I am counter object number 1 of 2  
> y.output # I am counter object number 2 of 2

Andrew!

Thank you. This makes it very clear. This particular arrangement makes  
clever use of instance variable. One is on the class instance and the  
other is on the each instance of that class.

And in the process of explaining this, you also indirectly explained one  
question that was lurking in my mind – are the  
attr\_readers/writers/accessors available to class variables – and as I  
understand it, they are – by way of your previous class \<\< self  
construct.

Now I can safely ignore any discussion on @@ variables in any book 😉

Thanks!  
-Kedar
