I like to make class files self-testing and can do so with the
==================== Junk.rb ==========================
CONST = 'a value'
# Unit tests
if __FILE__ == $0
===================== test/TestJunk.rb ==================
require 'Junk' unless defined?(CONST)
class TestJunk < Test::Unit::TestCase
assert(<<some assertion>>, "Some message")
The thought is that I can run Junk.rb or TestJunk.rb as a top-level
script and have the unit tests run either way.
This works, EXCEPT if I run Junk.rb it complains that I have already
assigned to CONST, so it is apparently loading Junk.rb twice.
I tried to avoid these with the if defined?(CONST) construct, but ot
does not help.
1. I thought the 'require' method would load the file only if it was
not loaded already. If so, how could two requires result in two
executions of the CONST = 'some value' line? There's something I'm not
2. Why doesn't the "if defined?(CONST)" thingy help? I would think it
is unnecessary given my assumption in 1. I'm definitely confused.
Ruby-Nuby in need of guidance.