Nice question.
Remember: When the Range
operator (..
or ...
) is used in a conditional statement, it does something totally unexpected: it doesn't create a Range
object. Instead, it acts as a "flip-flop" operator.
The below code actually
while gets
print if /start/../end/
end
by default is
while gets
# gets_input I put just to make the code more expressive
# actually the input taken using gets method applied here implicitly.
print if /start/ =~ gets_input .. /end/ =~ gets_input
end
Let me proof you. I took the help of Ruby Tracer
class.
trace = TracePoint.new do |tp|
p [tp.lineno, tp.event, tp.defined_class,tp.method_id]
end
trace.enable do
while gets
# when you type start in your console, 11 will be output.
print 11 if /start/../end/
end
end
Let me run this code, to show you my above code as a proof and also the Ruby Flip-Flop feature :
(arup~>Ruby)$ ruby test.rb
test.rb:6: warning: regex literal in condition
test.rb:6: warning: regex literal in condition
[4, :b_call, nil, nil]
[5, :line, nil, nil]
[5, :c_call, Kernel, :gets]
[5, :c_call, ARGF.class, :gets]
end # I presses **end** here.
[5, :c_return, ARGF.class, :gets]
[5, :c_return, Kernel, :gets]
[6, :line, nil, nil]
# Regexp#=~ call begin happened for /end/ =~ gets_input
[6, :c_call, Regexp, :=~]
# Regexp#=~ call end happened for /end/ =~ gets_input
[6, :c_return, Regexp, :=~]
[5, :c_call, Kernel, :gets]
[5, :c_call, ARGF.class, :gets]
start # I presses **start** here.
[5, :c_return, ARGF.class, :gets]
[5, :c_return, Kernel, :gets]
[6, :line, nil, nil]
# Regexp#=~ call begin happened for /start/ =~ gets_input
[6, :c_call, Regexp, :=~]
# Regexp#=~ call end happened for /start/ =~ gets_input
[6, :c_return, Regexp, :=~]
# Regexp#=~ call begin happened for /end/ =~ gets_input
[6, :c_call, Regexp, :=~]
# Regexp#=~ call end happened for /end/ =~ gets_input
[6, :c_return, Regexp, :=~]
[6, :c_call, Kernel, :print]
[6, :c_call, IO, :write]
[6, :c_call, Fixnum, :to_s]
[6, :c_return, Fixnum, :to_s]
# As there is a match so **if** clause true, thus 11 printed
11[6, :c_return, IO, :write]
[6, :c_return, Kernel, :print]
[5, :c_call, Kernel, :gets]
[5, :c_call, ARGF.class, :gets]
end
[5, :c_return, ARGF.class, :gets]
[5, :c_return, Kernel, :gets]
[6, :line, nil, nil]
[6, :c_call, Regexp, :=~]
[6, :c_return, Regexp, :=~]
[6, :c_call, Kernel, :print]
[6, :c_call, IO, :write]
[6, :c_call, Fixnum, :to_s]
[6, :c_return, Fixnum, :to_s]
11[6, :c_return, IO, :write]
[6, :c_return, Kernel, :print]
[5, :c_call, Kernel, :gets]
[5, :c_call, ARGF.class, :gets]