Den 16. mai. 2006 kl. 02:07 skrev ara.t.howard / noaa.gov:

> On Tue, 16 May 2006, Sy Ali wrote:
>
>> On 5/14/06, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov> wrote:
>>> well, if you have non-test code you can do whatever you like to  
>>> order it
>>
>> Not really.  All the testing code will be run after other code,
>> nomatter what you do.. even using begin/end blocks wouldn't work for
>> me.  All tests would run afterwards.  It was quite wierd.  Only the
>> testrunner stuff ended up working.
>
> can you show us this?  it works for me:
>
>   harp:~ > cat a.rb
>   BEGIN{ puts '======== BEGIN ========' }
>
>   END{ puts '======== END ========' }
>
>   require 'test/unit'
>
>   class ATest < Test::Unit::TestCase
>     def test_a() assert true end
>   end
>
>
>   harp:~ > ruby a.rb
>   ======== BEGIN ========
>   Loaded suite a
>   Started
>   .
>   Finished in 0.000684 seconds.
>
>   1 tests, 1 assertions, 0 failures, 0 errors
>   ======== END ========
>

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. In Ruby, require is an executable  
statement, that may have side effects, and thus ordering becomes  
significant:

bash$ cat foo.rb
puts "bar"

bash$ irb
irb(main):001:0> require "foo.rb"
bar
=> true
irb(main):002:0>


/Vidar