From: Robert Dober Date: 2006-04-12T06:01:15+09:00 Subject: Re: Equivalent of Java static code block in Ruby? ------=_Part_10819_5593243.1144789271120 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 4/11/06, Wes Gamble wrote: > > All, > > Is there a notion of a class level "static" code block in Ruby that > would get executed whenever the class is loaded? Basically the > equivalent of Java's "static {...}" construct? > > I don't necessarily want to put this in an initialize method because > it's just loading up static data from configuration files. > > Or do I have to put the code into an initialize method in my class and > then guard it so that it's only executed once? > > Thanks, > Wes > > -- > Posted via http://www.ruby-forum.com/. Well unless I missunderstood this is an easy one Part I In order to execute something when the class is loaded you just put it there ;) The attr_* methods are a good example, they are executed when the class statement is executed and their receiver is of course the class itself. So that would be like class Foo puts "#{self}Bar" @bar =3D "foo" ... Part II def statements are of course defining instance methods so what if we want static methods (well there is no such thing), class methods there are at least 3 options ... class << self def static or def self.static or def Foo.static # not sure if it works is a maintainence nightmare anyway !!! Hope that helps Robert -- Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_10819_5593243.1144789271120--