How A Ruby Case Statement Works And What You Can Do With It

by Alan Skorkin on August 24, 2009

CaseI found case statements in Ruby pretty interesting as they are capable of doing a little more than the equivalent constructs in other languages. We all know how a simple case statement works, we test on a condition that we give to a case statement, we then walk through a set of possible matches each of which is contained in a when statement e.g.:

print "Enter your grade: "
grade = gets.chomp
case grade
when "A"
  puts 'Well done!'
when "B"
  puts 'Try harder!'
when "C"
  puts 'You need help!!!'
else
  puts "You just making it up!"
end

So far nothing special, the above works just the way you would expect. But, you can do more with a case statement in Ruby.

Multi-Value When And No-Value Case

Ruby allows you to supply multiple values to a when statement rather than just one e.g.:

print "Enter your grade: "
grade = gets.chomp
case grade
when "A", "B"
  puts 'You pretty smart!'
when "C", "D"
  puts 'You pretty dumb!!'
else
  puts "You can't even use a computer!"
end

Pretty cool, but nothing too revolutionary. It doesn’t end there though you can use a case statement without giving it a value to match against, which allows a case statement to mimic the behavior of an if statement, e.g.:

print "Enter a string: "
some_string = gets.chomp
case
when some_string.match(/\d/)
  puts 'String has numbers'
when some_string.match(/[a-zA-Z]/)
  puts 'String has letters'
else
  puts 'String has no numbers or letters'
end

So, why would you use it instead of an if statement? Well, there is probably no reason, they are equivalent. But, it is good to be aware that you can do this, especially if you run into it in the wild, wouldn’t want to be caught unawares.

It All Evaluates To An Object

You probably keep hearing this over and over, but everything in Ruby works with objects. Things are no different when it comes to case statements. A case statement will always return a single object, just like a method call. So, you can safely wrap a case statement in a method call and Ruby will have no problems with it. For example, the above version of the case statement ca be re-written like this:

print "Enter a string: "
some_string = gets.chomp
puts case
when some_string.match(/\d/)
  'String has numbers'
when some_string.match(/[a-zA-Z]/)
  'String has letters'
else
  'String has no numbers or letters'
end

We are now wrapping the whole case statement in a puts method call, rather than doing it within each individual when statement. This works because no matter which when/else succeeds, the whole case statement returns the result of the last line that was executed (in our case it just always returns a string), and so the puts just writes out whatever string is returned.

How It All Works Under The Hood

You can almost consider case/when statements to be syntactic sugar for a method call. Every object in Ruby inherits a method called the case equality method, also known as the triple equals (===). You can think of it as an operator if it helps (the === operator), but we know that Ruby operators are just syntactic sugar for method calls. So whenever a when is trying to match a value (except when we are using a no-value case) there is a method call behind the scenes to the === method/operator. We can therefore take our very first example and re-write it using if statements to be completely equivalent to a case statement:

print "Enter your grade: "
grade = gets.chomp
if "A" === grade
  puts 'Well done!'
elsif "B" === grade
  puts 'Try harder!'
elsif "C" === grade
  puts 'You need help!!!'
else
  puts "You just making it up!"
end

The implications of this are as follows. Any class you write can override the === method and therefore define it’s own behavior for usage in a case statement. For built-in objects such as strings, the === operator/method is simply equivalent to the == method/operator. But you don’t have to be restricted to this. If you want your class to have the ability to participate in a case statement in some specialized way all you need to do is something like this:

class Vehicle
  attr_accessor :number_of_wheels
 
  def initialize(number_of_wheels)
    @number_of_wheels = number_of_wheels
  end
 
  def ===(another_vehicle)
    self.number_of_wheels == another_vehicle.number_of_wheels
  end
end
 
four_wheeler = Vehicle.new 4
two_wheeler = Vehicle.new 2
 
print "Enter number of wheel for vehicle: "
vehicle = Vehicle.new gets.chomp.to_i
 
puts case vehicle
when two_wheeler
  'Vehicle has the same number of wheels as a two-wheeler!'
when four_wheeler
  'Vehicle has the same number of wheels as a four-wheeler!'
else
  "Don't know of a vehicle with that wheel arrangement!"
end

In this case even though we are matching directly on the vehicle object in our case/when statement, behind the scenes a method call is made to the === operator and so the real match is made on the number of wheels attribute that the vehicle object has (as per what is defined in the === method).

This idea of hiding method calls behind syntax that doesn’t look like method calls, seems to be rather common in Ruby. Personally I like it, you can do some rather elegant things, but the building blocks are still just a bunch of method calls. When it comes to the case statement though, I hardly ever use it when I program in Java for example, but it looks to me like it can be somewhat more handy in Ruby especially with the ability to define custom case behavior for your own objects.

Image by dahliascakes

{ 4 trackbacks }

Plough through Ruby
August 25, 2009 at 4:39 am
Dew Drop – August 25, 2009 | Alvin Ashcraft's Morning Dew
August 25, 2009 at 10:54 pm
Kai’s daily tech #28 … | Kai Richard König
September 5, 2009 at 6:07 am
Comparing Equality (==, eql?, equal?) and Case Equality (===) in Ruby – Refactoring Thoughts
December 9, 2009 at 4:45 pm

{ 12 comments… read them below or add one }

FrihD August 24, 2009 at 9:46 pm

Also, “when some_class ” tests the class of the object passed in the case statement. e.g.
klass = Class.new
case klass.new
when klass
p “hurrah”
end

Reply

Alan Skorkin August 24, 2009 at 9:58 pm

Hmm, I am not precisely sure what you mean, could you elaborate?

Reply

FrihD August 25, 2009 at 12:47 am

Instead of having a lot of “is_a? Foo” you can do:

case obj
when Foo
do_something
when Bar
do_anotherthing
end

It can be useful when you implement e.g. a Request class with many subclasses.
(but at some point, a hash becomes easier to manage). It is also useful when you want to get rid of duck_typing in a method or simulate method overloading.

Reply

Alan Skorkin August 25, 2009 at 12:05 pm

Ahh yes I get it now, my apologies for being a little slow :). That is definitely very handy.

Reply

John F. Miller August 25, 2009 at 2:12 am

Another Way of saying this is that by default Class#=== checks whether target#kind_of? evaluates to true for the given class. This is very useful when dealing with a method that can take many different types of arguments:

def foo(input)
case input
when String
JSON.parse input
when Hash
input
when Numeric
{:value=>input}
when NilClass
{}
end
# …
end

Reply

Alan Skorkin August 25, 2009 at 12:07 pm

That clarifies it even more. Definitely some great use cases for case :).

Reply

smeally January 5, 2010 at 11:06 am

Hey there this is a great post

Reply

namelessjon August 24, 2009 at 10:52 pm

Your example with the regexes can actually be written as:

case some_string
when /\d/
‘String has numbers’
when /[a-zA-Z]/
‘String has letters’
else
‘String has no numbers or letters’
end

Reply

Alan Skorkin August 24, 2009 at 11:08 pm

Yes, very true.

I wrote it the way I did to illustrate the fact that you can use a case to mimic an if by not providing the case with a value.

Reply

lucas August 25, 2009 at 1:11 pm

I was writing a state machine in ruby the other day using a case statement to fork off the work to various methods based on which :symbol was assigned to @state

case @state
when :state_1
state_1(some_arg)
when :state_2
state_2(some_arg)
when :state_3
state_3(some_arg)
when :state_4
state_4(some_arg)
else
raise “invalid state”
end

Then I had it pointed out to me that the whole thing was unnecessary and could better be written as:

self.send @state, some_arg

Ruby is awesome like that :)

Reply

Alan Skorkin August 25, 2009 at 5:03 pm

How cool is it :), as I dig deeper into Ruby I keep discovering things that just blow me away sometimes, not that Ruby doesn’t have it’s share of issues, but still :).

Reply

Akhil Bansal April 19, 2010 at 8:34 pm

Interesting, I didn’t knew it.

Reply

Leave a Comment

Previous post:

Next post: