From: James Britt Date: 2006-06-10T22:49:44+09:00 Subject: Re: Ruby Class question Peer Allan wrote: > Justin Collins wrote: > > >>James already answered the question pretty well, I just want to address >>the "when does that line get executed?" question. >>It is executed when the class is 'parsed'. For example: >> >>irb(main):001:0> class A >>irb(main):002:1> puts "Hello!" >>irb(main):003:1> end >>Hello! >>=> nil >> >> > > > Thanks for the explanations. This is what I was looking for. I did not > realize that the code within the class definition gets executed, rather > than just defined. Neat! It's a subtle point, though. For example, there have been threads here on some issues that arise when you do not control the order in which files are loaded. You may see this idiom a lot, too: if __FILE__ == $0 # Some code ... end This checks to see if the file has been loaded directly, rather than being included in another file. It is a way to wrap stuff (such as unit tests or command line interaction) that should not execute if the source file is being used by as part of a larger application -- James Britt http://www.ruby-doc.org - Ruby Help & Documentation http://www.artima.com/rubycs/ - The Journal By & For Rubyists http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.30secondrule.com - Building Better Tools