Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a switch statement in Ruby

How do I write a switch statement in Ruby?

like image 994
readonly Avatar asked Jun 04 '09 01:06

readonly


People also ask

Does Ruby have a switch statement?

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. Note: In other programming languages this is known as a switch statement.

Can you use || IN CASE statement Ruby?

If you are eager to know how to use an OR condition in a Ruby switch case: So, in a case statement, a , is the equivalent of || in an if statement.

What is a case statement in Ruby?

The case statement is a multiway branch statement just like a switch statement in other languages. It provides an easy way to forward execution to different parts of code based on the value of the expression.


2 Answers

Ruby uses the case expression instead.

case x when 1..5   "It's between 1 and 5" when 6   "It's 6" when "foo", "bar"   "It's either foo or bar" when String   "You passed a string" else   "You gave me #{x} -- I have no idea what to do with that." end 

Ruby compares the object in the when clause with the object in the case clause using the === operator. For example, 1..5 === x, and not x === 1..5.

This allows for sophisticated when clauses as seen above. Ranges, classes and all sorts of things can be tested for rather than just equality.

Unlike switch statements in many other languages, Ruby’s case does not have fall-through, so there is no need to end each when with a break. You can also specify multiple matches in a single when clause like when "foo", "bar".

like image 63
Chuck Avatar answered Oct 05 '22 23:10

Chuck


case...when behaves a bit unexpectedly when handling classes. This is due to the fact that it uses the === operator.

That operator works as expected with literals, but not with classes:

1 === 1           # => true Fixnum === Fixnum # => false 

This means that if you want to do a case ... when over an object's class, this will not work:

obj = 'hello' case obj.class when String   print('It is a string') when Fixnum   print('It is a number') else   print('It is not a string or number') end 

Will print "It is not a string or number".

Fortunately, this is easily solved. The === operator has been defined so that it returns true if you use it with a class and supply an instance of that class as the second operand:

Fixnum === 1 # => true 

In short, the code above can be fixed by removing the .class from case obj.class:

obj = 'hello' case obj  # was case obj.class when String   print('It is a string') when Fixnum   print('It is a number') else   print('It is not a string or number') end 

I hit this problem today while looking for an answer, and this was the first appearing page, so I figured it would be useful to others in my same situation.

like image 31
kikito Avatar answered Oct 05 '22 21:10

kikito