Main‎ > ‎


posted Feb 18, 2010, 11:45 AM by Zeriab ,
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"Time must be a numeric")
    @titel = titel
    @time = time
  def <=>(medal)
    return time <=> medal.time
class TestError < StandardError; end
bronze ='Bronze', 180)
bronze2 ='Bronze', 180)
bronze3 ='Bronze3', 180)
silver ='Silver', 105)
gold ='Gold', 87.5)
unless (bronze == bronze2)
  raise"Medal.== does not compare #{bronze.inspect} " +
                      "and #{bronze2.inspect} properly")
unless (bronze == bronze3)
  raise"Medal.== does not ignore the title")
unless (silver <=> bronze) == -1
  raise"Medal.<=> does not return -1 when its time is less" +
                      " than the medal compared to")
unless (silver <=> gold) == 1
  raise"Medal.<=> does not return 1 when its time is greater" +
                      " than the medal compared to")
unless (bronze <=> gold) == 1
  raise"Medal.<=> does not preserve transitivity.\n" +
                      "I.e. <=> is highly problematic.")
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.