Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Operators and Control Structures Ruby Control Structures The Ruby Case Statement

Does the ruby case statement only work with strings? How come the following code (see post) does not work?

Hey everyone,

I was wondering - does the ruby case statement only work with strings?

How come the following code does not seem to work?

print "Enter number: " number = gets.chomp

case number when number > 1 puts "greater than 1" when number == 1 puts "equal to 1" else puts "less than 1" end

I've also tried:

print "Enter number: " number = gets.chomp.to_i

case number when > 1 puts "greater than 1" when == 1 puts "equal to 1" else puts "less than 1" end

Still doesn't work. Any suggestions? Thank you!

Matt Coston - Thanks for the quick reply - I just tried that code and for some reason it always goes to the else statement no matter what number I enter.

If I enter 2, it still says "less than 1" If I enter 1, it still says "less than 1"

4 Answers

Well case can work with integers. But I think case statment only works for equality comparison. If case mathces the value after when then it will do the code for the when branch.

I was getting errors with the code to start off with. It does seem to always go to the else reply. I am taking a look at it right now.

This code here is fully functional:

number = ""
print "Enter number: " 
number = gets.chomp.to_i
if number > 1
    puts "#{number} is greater than 1"
else if number == 1 then
    puts "#{number} is equal to 1" 
else puts " #{number} is less than 1"
end
end

It appears case works really well with strings and quickly making longer if statements.

I liked this code example to explain it.

car = "Patriot"

manufacturer = case car
   when "Focus" then "Ford"
   when "Navigator" then "Lincoln"
   when "Camry" then "Toyota"
   when "Civic" then "Honda"
   when "Patriot" then "Jeep"
   when "Jetta" then "VW"
   when "Ceyene" then "Porsche"
   when "Outback" then "Subaru"
   when "520i" then "BMW"
   when "Tundra" then "Nissan"
   else "Unknown"
end

puts "The " + car  + " is made by "  + manufacturer

HI I think I can shed some light on this for you. The case statement in Ruby is a little different than in something like perl or bash scripting. The case method is used mainly for matching like the example Matt Coston said he liked as an explanation for case. It is also possible to use case statements as a substitute for if/elsif/else statement. A case statement without giving it a value to match against, which allows a case statement to mimic the behavior of an if statement, Example hour = 15

case when hour < 12 puts "Good Morning" when hour > 12 && hour < 17 puts "Good Afternoon" else puts "Good Evening" end

outputs "Good Afternoon"