From: Patrick Hurley Date: 2006-04-14T02:10:31+09:00 Subject: Re: Equivalent of Java static code block in Ruby? On 4/13/06, azrael@cream.org wrote: > > > > I think all you would do by adding another division here, is confuse > > matters. Currently I think it's pretty clear. > > > > I think I agree with you. One shouldn't do stuff simply because that's > the way Java does it. If anything, quite the opposite. > > Although, it's not true that all code in a class is executed at class > load time. Most of the code typically will only be called on > instantiation of an object, and sometimes it's helpful not to mix the > two up too thoroughly. > > Martin > > Actually the code in those method bodies is executed -- that is what defines the methods. Think about this (very silly) sample code: class Foo if (Time.now.to_i % 2).zero? def bar puts "Even time" end else def bar puts "Odd time" end end end f = Foo.new f.bar Note that it does not matter when you call bar, but when the class is loaded... pth