Switch as else if in Ruby

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: Ruby

In Ruby, you can use switch statements to replace multiple if/else statements, which makes your code more readable and maintainable. The syntax for a switch statement in Ruby is:

case expression
when value1
  # code to run if expression equals value1
when value2
  # code to run if expression equals value2
else
  # code to run if expression doesn't match any of the values
end

Here is an example of how to use switch statements as an alternative to multiple if/else statements:

x = 4

case x
when 1
puts "x is 1"
when 2
puts "x is 2"
when 3
puts "x is 3"
when 4
puts "x is 4"
else
puts "x is not 1, 2, 3, or 4"
end

In the above example, the value of x is tested against each when statement. If x matches the value provided, then the corresponding code block is executed. Alternatively, if none of the when values match the value of x, then the else block is executed.

You can also use ranges and regular expressions in your switch statements, allowing for further flexibility. Here's an example:

age = 25

case age
when 0..17
puts "You're too young to drive"
when 18..21
puts "You can vote, but not drink"
when 21..25
puts "You can drink, but not rent a car"
else
puts "You can do everything!"
end

In this example, the age variable is checked against multiple ranges. If age falls within the specified ranges, the corresponding statement is printed. If it doesn't match any of the ranges, then the else statement is printed.