I find this behavior of the BEGIN and END block to be rather strange:



[gus@comp tests]$ cat BEGIN.rb
#! /usr/bin/ruby

BEGIN { puts("General begin") }
END { puts("General end") }

puts("Main")

if $0 == __FILE__
  BEGIN { puts("Protected begin") }
  END { puts("Protected end") }
end

[gus@comp tests]$ ruby BEGIN.rb
General begin
Protected begin
Main
Protected end
General end

So far, so good.

[gus@comp tests]$ cat test_BEGIN.rb
load 'BEGIN.rb'
[gus@comp tests]$ ruby test_BEGIN.rb
General begin
Protected begin
Main
General end

There I don't understand. The behavior I was expected is that none of
the 'protected' blocks would be run. A behavior I could understand is
that both of them are run. But that one is run and not the other is
strange, isn't it?

Testes with two verion of ruby:
gus@comp tests]$ ruby -v
ruby 1.8.1 (2004-04-05) [i686-linux]
[gus@comp tests]$ /usr/local/bin/ruby -v
ruby 1.9.0 (2004-05-21) [i686-linux]

Guillaume.