Ruby case Statement (The switch Statement) Tutorial

In this section, we will learn what the case statement is and how to use it in Ruby.

What is case Statement in Ruby?

The case statement in Ruby acts like an if-elsif-else statement where the case statement takes an input value and checks that value against multiple inner values (we define these values in the body of the case statement). Now each of the inner values has a block associated with them and the first inner value that matched the value of the case’s input, its’ block of instructions, will be executed.

Note: for those of you who are coming from C type languages, the case statement is like the switch statement you’ve seen in those languages.

Let’s jump into the syntax of this statement and there we will explain how it works in more details.

Ruby case Statement Syntax:

case input_value

when value1 
    #instructions
when value2 
    #instructions
when value_n
    #instructions
else
    #instructions 
end 

Ruby when Statement

Within the body of the `case` statement, we can add one or more `when` clause. Each of these `when` clauses will be matched against the input value of the case statement and the first one that matched the value, the instructions within its body will run and the rest of `when` clauses will be skipped (if there are more remained).

Ruby else Statement

Also, a case statement can have an optional `else` clause as well. The body of the `else` statement will run only if none of the `when` clauses in the case statement matched the input value!

Note: the use of else statement here is exactly the same as when we use it in an if statement.

Example: using case statement in Ruby

val = gets.to_i

case val 

when 1
    puts "You've selected the value 1"
when 2
    puts "You've selected the value 2"
when 3
    puts "You've selected the value 3"
when 4
    puts "You've selected the value 4" 
when 5
    puts "You've selected the value 5"
else 
    puts "The value is not clear"
end 

Output:

Please enter a value between 1 to 5

5

You've selected the value 5

How Does the case Statement Work in Ruby?

In this example, we’re asking a user to enter a value between 1 to 5. Then we’ve passed the value to the case statement and inside the body of this statement we’ve matched the input value against five `when` clauses to see which one matches the input value.

For this particular example, we entered the value 5 and so the last `when` clause matched the value and so its body, which is just a call to the `puts` method, ran.

Also note that if none of the `when` clauses in the body of the case statement matched the input value, in that case, if there’s an `else` statement, it will be run.

Ruby case Statement and Return value

You should know that the case statement in Ruby is capable of returning a value as well!

Basically, the case statement uses the result of the last expression in the body of the `when` clauses that matched the input value and returns the result of that expression as its final result.

Let’s see the example below to see how it’s working.

Example: using case statement to return a value in Ruby

puts "Please enter a value between 1 to 5"

val = gets.to_i

result = case val 

when 1
    "You've selected the value 1"
when 2
    "You've selected the value 2"
when 3
     "You've selected the value 3"
when 4
    "You've selected the value 4" 
when 5
    "You've selected the value 5"
else 
    "The value is not clear"
end 

puts result

Output:

Please enter a value between 1 to 5

5

You've selected the value 5

Note that in this example, none of the `when` clauses used the `puts` method in their body! They just have one expression and that is a string value.

So here we’ve passed the value 5 as the input value to the program. For this reason, the last `when` clause of the case statement matched the input value and the only expression in this `when` statement returned as a result of calling the `case` statement.

Note that the `case` statement is called on the right side of the assignment operator and so the result of this statement is passed to the `result` variable.

That’s why when we called `puts result` statement, we get the same value that was defined in the body of the `when 5` clause.

Ruby when Statement with Multiple Values

Note that the when clause in the case statement is not limited to just one value! In fact, you can set multiple values for each when clause (each value is separated using a comma). In that case, the input value will be matched against each value of the when clause to see if one of those values matches the input value of the case statement.

Note: we can use range to create a sequence of numbers for each when clause as well.

Example: using range of values for the when statement in Ruby

puts "Please enter a value between 1 to 100"

val = gets.to_i

result = case val 

when 1,2,3,4,5,6,7,8,10
    "Your value is between 1 to 10"
when 10..50
    "Your value is between 10 to 50"
when 50..100
     "Your value is between 50 to 100"
else 
    "The value is not clear"
end 

puts result

Output:

Please enter a value between 1 to 100

60

Your value is between 50 to 100

Example: using case statement without the else statement

first_name = "John"
case first_name
when "Jack"
    puts "The name is Jack"
when "Omid"
    puts "The name is Omid"
when "John"
    puts "The name is John"
end 

Output:

The name is John

Note that we didn’t use the `else` statement in this example, which shows that the use of else statement is optional in the case statement.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies