Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ruby's case statement fall through?

Tags:

I am writing a hangman game in ruby and I wanted to use a case statement to determine which body part to place corresponding to a number of incorrect guesses. I made this game using a board class I use for other games like chess and connect-4 because I have a method which serializes the board class allowing me to save and load the game without any extra code. For the game to be saved, I needed some way of determining the number of incorrect guesses for the hangman without adding extra variables to the board class. To solve this I used an instance variable on the board class called history, which can be used to push moves from the game to the boards history. When the board gets serialized, the history is saved as well, which can be read by the game and used to determine incorrect guesses.

In the hangman game, I have a method called read history (which I use for all the games since it solves the serialization issue described above). The read_history method is responsible for reading the past guesses, display them, and determine the number of incorrect guesses. This number is then passed to a hang method which determines which body parts of the hangman to add.

def hang(incorrect)     case incorrect     when 0          @hangman = ["   ", "   ", "   "]         break     when 7          @hangman[2][2] = '\\'     when 6         @hangman[2][0] = '/'     when 5         @hangman[2][1] = '*'     when 4         @hangman[1][2] = '\\'     when 3         @hangman[1][0] = '/'     when 2          @hangman[1][1] = '|'     when 1         @hangman[0][1] = 'o'     end end 

If I were writing this in java, and a value of 5 were passed to the above method, it would read the statement until it hit "when 5" or in java terms "case 5:". It would notice that there is not a break in the statement and will move down the list executing the code in "case 4:" and repeating until a break is found. If 0 were passed however it would execute the code, see the break, and would not execute and other statements.

I am wondering if Ruby is capable of using case statements the way java does in the way that they fall through to the next statement. For my particular problem I am aware that I can use a 0.upto(incorrect) loop and run the cases that way, but I would like to know the similarities and differences in the case statement used in ruby as opposed to the switch-case used in java

like image 583
Jared Archey Avatar asked Jul 16 '15 01:07

Jared Archey


People also ask

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.

Does Ruby have a switch statement?

Ruby uses the case for writing switch statements. As per the case documentation: Case statements consist of an optional condition, which is in the position of an argument to case , and zero or more when clauses.


2 Answers

No, Ruby's case statement does not fall through like Java. Only one section is actually run (or the else). You can, however, list multiple values in a single match, e.g. like this site shows.

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 

It's functionally equivalent to a giant if-else. Code Academy's page on it recommends using commas to offer multiple options. But you can still won't be able to execute more than one branch of logic.

like image 172
Martin Avatar answered Oct 15 '22 23:10

Martin


It does not fall through.

Ruby just doesn't have the same behavior as Java for this type of statement.

If you want to simulate the fall through behavior, you can do something like this:

def hang(incorrect)     @hangman = ["   ", "   ", "   "]     @hangman[2][2] = '\\' if incorrect > 6     @hangman[2][0] = '/' if incorrect > 5     @hangman[2][1] = '*' if incorrect > 4     @hangman[1][2] = '\\' if incorrect > 3     @hangman[1][0] = '/' if incorrect > 2     @hangman[1][1] = '|' if incorrect > 1     @hangman[0][1] = 'o' if incorrect > 0      @hangman   end 
like image 20
msergeant Avatar answered Oct 15 '22 21:10

msergeant