I 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.:
ruby
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.:
ruby
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.:
ruby
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:
ruby
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:
ruby
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:
```ruby 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