Skip to main content
Updated with endless and beginless ranges
Source Link
Paulo Belo
  • 4.5k
  • 1
  • 27
  • 22

If you need "less than" or "greater than":

case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when 7..1.0/0 "It's equal or greater than 7" when -1.0/0..0 "It's equal or less than 0" end 

1.0/0 is equal to Float::INFINITY, so you can use which you prefer.

After Ruby 2.6 you can use Endless Ranges, After Ruby 2.7 you can also use Beginless Ranges, so you can do for example:

case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when (7..) "It's equal or greater than 7" when (..0) "It's equal or less than 0" end 

If you need "less than" or "greater than":

case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when 7..1.0/0 "It's equal or greater than 7" when -1.0/0..0 "It's equal or less than 0" end 

1.0/0 is equal to Float::INFINITY, so you can use which you prefer.

If you need "less than" or "greater than":

case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when 7..1.0/0 "It's equal or greater than 7" when -1.0/0..0 "It's equal or less than 0" end 

1.0/0 is equal to Float::INFINITY, so you can use which you prefer.

After Ruby 2.6 you can use Endless Ranges, After Ruby 2.7 you can also use Beginless Ranges, so you can do for example:

case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when (7..) "It's equal or greater than 7" when (..0) "It's equal or less than 0" end 
Source Link
Paulo Belo
  • 4.5k
  • 1
  • 27
  • 22

If you need "less than" or "greater than":

case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when 7..1.0/0 "It's equal or greater than 7" when -1.0/0..0 "It's equal or less than 0" end 

1.0/0 is equal to Float::INFINITY, so you can use which you prefer.