Posted April 27, 2021 ‐  5 min read

How to write a switch statement in Ruby

In Ruby, the classic "switch statement" is possible with the case/when keywords. Whenever if statements become too long and start looking messy you might want to consider using the case statement to write cleaner code.

So how do you switch to case/when ?

Case statements in Ruby are very powerful. To quote the popular ProgrammingRuby book, "The Ruby case expression is a powerful beast: a multiway if on steroids."

Let's begin by converting a small-sized if expression into a case/when statement. The following if expression

if name == "Peter Parker"
  hero_name = "Spiderman"
elsif name == "Bruce Wayne"
  hero_name = "Batman"
  hero_name = "unknown"

will look like this

case name
when "Peter Parker"
  hero_name = "Spiderman"
when name == "Bruce Wayne"
  hero_name = "Batman"
  hero_name = "unknown"

Important keywords

There are 3 important keywords that are used in the case statement:

case operates by comparing the target (the expression after the keyword case) with each of the comparison expressions after the when keywords. This test is done using comparison === target.

The key differences to note are that you no longer need to write the comparison operator or the thenkeyword. If you have any background with another programming language then you may have also noticed that with Ruby you don't need to end every individual case with a break clause.

Indentation of the case/when statement in Ruby

There is no official standard about this, however developers tend to put the when keyword on the same column as case (source : stackoverflow) :

case bar
when 4 then ...
when 2 then ...
else ...

Which is quite logical : think about the if/elsif indentation.

Case/When in Ruby : Advanced used... cases

Now it’s time to see more advanced use cases of the case expression. Inside your when clauses, you can do much more than single value comparisons. With case you can compare ranges, classes, regular expressions, multiple values, and much more. The following is a comprehensive example of the many ways you can use a case statement :

def switch_me(something)
  case something
  when /^1/ 
    "param starts with one" # return keyword is optional
  when 80..90
  when 50 then "param is 50" #then keyword allows return on same line
  when 10, 20
    "param is either 10 or 20"
  when String
    "You passed a String"

In your Ruby console :

$> switch_me("111")
=> "param starts with one"

$> switch_me(82)
=> "B+"

$> switch_me(50)
=> "param is 50"

$> switch_me(10)
=> "param is either 10 or 20"

$> switch_me("20")
=> "You passed a String"

$> switch_me(42)
=> nil

Let's walk through each individual case:

And finally, in our last when clause we check if the first param is a String object. You may have noticed that there is a puts keyword before the case statement. This is possible because as with if, case returns the value of the last expression executed so it can be input to a method call.

Another interesting way is to use the case statement like an if-else statement. You cannot give a value to case if using it like this :

def case_like_ifelse(param1)
  when param1 == 1, param1 == 2
    "param1 is one or two"
  when param1 == 3
    "param1 is three"

In your Ruby console :

$> case_like_ifelse(1)
=> "param1 is one or two"

$> case_like_ifelse(3)
=> "param1 is three"

$> case_like_ifelse(3)
=> "unknown"

Now you’re all set to write complex switch control expressions in Ruby!

Help needed 😊

If you enjoyed the article, you can :

  • Share the article on Twitter , or LinkedIn , or Reddit , it will stimulate the writing effort, thanks !
  • Subscribe to the newsletter, you'll be warned each time a new Rails tutorial is released : we start from the rails new command to fully understand a new concept.
  • Subscribe to the Bootrails Beta, it's a tool to launch new Rails apps.

Thanks to all,