__END__
Post date: Feb 18, 2010 7:45:26 PM
Here's a little mention about an interesting special term, __END__.
The rest of the section after that will not be run. In fact I believe the parser will stop at __END__ and therefore won't even build the abstract syntax tree for what comes afterwards. (An effect is the lack of syntax checking)
This can be used for preventing the inclusion of test code except when you want to use it.
class Medal
include Comparable
attr_reader :titel, :time
def initialize(titel, time)
unless time.is_a?(Numeric)
raise ArgumentError.new("Time must be a numeric")
end
@titel = titel
@time = time
end
def <=>(medal)
return time <=> medal.time
end
end
__END__
class TestError < StandardError; end
bronze = Medal.new('Bronze', 180)
bronze2 = Medal.new('Bronze', 180)
bronze3 = Medal.new('Bronze3', 180)
silver = Medal.new('Silver', 105)
gold = Medal.new('Gold', 87.5)
unless (bronze == bronze2)
raise TestError.new("Medal.== does not compare #{bronze.inspect} " +
"and #{bronze2.inspect} properly")
end
unless (bronze == bronze3)
raise TestError.new("Medal.== does not ignore the title")
end
unless (silver <=> bronze) == -1
raise TestError.new("Medal.<=> does not return -1 when its time is less" +
" than the medal compared to")
end
unless (silver <=> gold) == 1
raise TestError.new("Medal.<=> does not return 1 when its time is greater" +
" than the medal compared to")
end
unless (bronze <=> gold) == 1
raise TestError.new("Medal.<=> does not preserve transitivity.\n" +
"I.e. <=> is highly problematic.")
end
p 'Medal testing completed successfully'
I am sure you can see how it can be helpful in providing test data for scripters while being ignored normally.
Of course you could use a if false ... end for similar effect.
The different is that the abstract syntax tree will be created while it will not when using __END__. On the other hand copy-pasting several sections into a single section may disable sections.