Ruby Case on Ranges
Case statements in Ruby are pretty cool because we can use ANYTHING that responds to a tree equal operator, ===. One of those is Range.
age = rand(1..100)
p age
case age
when -Float::INFINITY..20
puts "You're too young."
when 21..64
puts "You're the right age."
when 65..Float::INFINITY
puts "You're too old."
end
Written by Lukas Alexandre
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#