From: ara.t.howard@... Date: 2006-05-16T23:17:55+09:00 Subject: Re: Forcing some code to run at the end of tests On Tue, 16 May 2006, Vidar Larsen wrote: > It looks like the ordering is important. If you do: > > require 'test/unit' > BEGIN{ puts '======== BEGIN ========' } > END{ puts '======== END ========' } > > class ATest < Test::Unit::TestCase > def test_a() assert true end > end > > then the result will be: > > ======== BEGIN ======== > ======== END ======== > Loaded suite a > Started > . > Finished in 0.001859 seconds. > > 1 tests, 1 assertions, 0 failures, 0 errors > > The problem is that include/import on top is idiomatic (even sometimes > necessary) in for instance C/C++ and Java, so it seems natural to carry that > over to Ruby. it's also idiomatic to have BEGIN/END blocks before require though, because BEGIN actually happens before any requires take place and END needs to occur (typically) even if require throws an exception. harp:~ > cat a.rb BEGIN{ p Queue rescue warn $! } END{ p Queue } require 'thread' raise harp:~ > ruby a.rb uninitialized constant Queue a.rb:4: unhandled exception Queue if you move the BEGIN/END blocks to the end of this script you will only get uninitialized constant Queue a.rb:4: unhandled exception which is very counter-intuitive. in any case it's good pratice to put BEGIN/END blocks up front imho. cheers. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama