Black Bytes
Share this post!

The Many Uses Of Ruby Case Statements

Whenever you need to use some if / elsif statements you could consider using a Ruby case statement instead. In this post, you will learn a few different use cases and how it all really works under the hood.

ruby case

Note: In other programming languages this is known as a switch statement.

The components of a case statement in Ruby:

Keyword Description
case Starts a case statement definition. Takes the variable you are going to work with.
when Every condition that can be matched is one when statement.
else If nothing matches then do this. Optional.

Ruby Case & Ranges

The case statement is more flexible than it might appear at first sight. Let’s see an example where we want to print some message depending on what range a value falls in.

I think this code is pretty elegant compared to what the if / elsif version would look like.

Ruby Case & Regex

You can also use regular expressions as your when condition. In the following example we have a serial_code with an initial letter that tells us how risky this product is to consume.

When Not to Use Ruby Case

When you have a simple 1:1 mapping, you might be tempted to do something like this.

In my opinion it would be better to do this instead:

The hash solution is more efficient and easier to work with. Don’t you think?

How case works: the === method

You may be wondering how case works under the hood. If we go back to our first example, this is what is happening:

As you can see, the condition is reversed because Ruby calls === on the object on the left. The === is just a method that can be implemented by any class. In this case, Range implements this method by returning true only if the value is found inside the range.

This is how === is implemented in Rubinius (for the Range class):


Procs + Case

Another interesting class that implements === is the Proc class.

Related post: Learn more about procs & lambdas.

In this example I define two procs, one to check for an even number, and another for odd.

This is what is really happening:

Using === on a proc has the same effect as using call.


You have learned how the Ruby case statement works and how flexible it can be. Now it’s your turn to start making the best use of it in your own projects.

I hope you found this article useful!

Please share this post so more people can learn! 🙂

Victor says last year

There are some more usages I’ve found useful:
* typecheck: case x; when Numeric ….
* define === method for your own class: case x; when…) …
* using case without checked variable, just as “structured conditions check”: case; when x > 1; … when something_happened …

John McCaffrey (@j_mccaffrey) says last year

Nice write up!

    Jesus Castello says last year

    Thank you 🙂

ALexey says last year

There is another cool feature with checking object’s class:

    Jesus Castello says last year

    Yeah this works because Module implements ===. Just be careful with this one, you want to use polymorphism if possible instead of checking on the class.

Alexey Pokhozhaev says last year

Ok, thanks!

danielpclark says last year

I’ve never seen a Proc defined that way proc(&:odd?). That’s pretty genius! ^_^

KRoot (@kraevroot) says last year

You can use case with arrays:

Robert says last year

Instead of proc(&:odd?) you can also use :odd?.to_proc.

Btw. odd and even should really be constants IMO.

Comments are closed