From: Kedar Mhaswade Date: 2011-01-07T04:45:41+09:00 Subject: Re: Class Initialization? Brian Candler wrote in post #972835: > Kedar Mhaswade 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 -- Posted via http://www.ruby-forum.com/.