From: Robert Klemme Date: 2006-03-21T20:03:49+09:00 Subject: Re: Class (not instance) initializers Bhrgunatha Deva wrote: > Newbie question: > > Is there an equivalent to a static constructor in ruby? > something like: > > class MyClass > # want this to run before any instances of MyClass are created > def class_initialize > self.some_complex_initialisation > end > end > > At the moment I'm using: > class MyClass > def initialize > @@init = self.some_complex_initialisation if @@init.nil? > end > end > > I'd love to know if there is a more rubyesque way of doing this Just place your code in the class body: class MyClass @class_state = complex_calculation end Often one needs to inform a super class of a new child. This can be done with #inherited: class Base def self.inherited(cl) print Base, " was inherited by ", cl, "\n" end end >> class Foo < Base >> end Base was inherited by Foo => nil Kind regards robert