This is an unwanted line 1.Normally, the typical way of segregating the wanted lines with the unwanted ones is probably via writing code that logically looks something like this:
This is an unwanted line 2.
This is a wanted line 1.
This is a wanted line 2.
This is an unwanted line 3.
This is an unwanted line 4.
in_header = false # variable to remember whether if in_headerBut in ruby, the idea of applying ranges on regular expression matches simplifies the syntax down to a bare minimum:
while line = STDIN.gets
if line =~/^START$/ # found header, start trapping
in_header = true
elsif line =~/^END$/ # end header, stop trapping
print line # have to print the line boundary inclusive
in_header = false
if in_header # still in boundary, print line
while line = STDIN.getsThis is one of the things that I have seen that's unlike any other language I've encountered before. Amazing and totally delightful!
if line =~ /^START$/ .. line =~ /^END$/