Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ruby Ranges As Conditional Expressions

DZone's Guide to

Ruby Ranges As Conditional Expressions

·
Free Resource
Source: Ruby Ranges - Techotopia [techotopia.com]

 
  
Ruby Ranges can also be used as conditional expressions in looping conditions. The range start value represents the start of the loop, which runs until the range end marker is detected.

  
while input = gets
  puts input + " triggered" if input =~ /start/ .. input =~ /end/
end


 
output (test performed within an IRB session):
test
ready
go
do it
start
start
 triggered
fun fun fun
fun fun fun
 triggered
rest
rest
 triggered
that's enough
that's enough
 triggered
stop
stop
 triggered
end
end
 triggered
sigh
hello?
^CIRB::Abort: abort then interrupt!!
Note: This syntax also works: while input = gets puts input + " triggered" if input[/start/] .. input[/end/] end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}