On Wed, Jan 5, 2011 at 3:33 PM, Kedar Mhaswade <kedar.mhaswade / gmail.com> 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
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/