Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Erik Nuber
Erik Nuber
20,629 Points

Case statement using and / or operators

I am wondering if CASE statements can also handle && / ||

I have tried and though it runs, it then always goes to the else statement. Since there isn't an actual console error, I am wondering it would thus be possible and I'm just missing something...

print "Enter name: "
name = gets.chomp

if name.upcase == "ERIK"
    puts "That's my name, too!"
  puts "Hi #{name}!"

print "Modify Your Name. Type 'uppercase' or 'reverse': "
answer = gets.chomp.upcase

case answer
when "UPPERCASE" || "UPPER" || "UP"
  puts "This is your name in uppercase: #{name.upcase}"
when "REVERSE" || "REV"
  puts "This is your name backwards: #{name.reverse}"
when "BOTH"
  puts "This is your name both backwards and in uppercase: #{name.upcase.reverse}"
  puts "Ok, maybe later"

2 Answers

Matt Coston
Matt Coston
18,425 Points

I am by no means a Ruby expert, but it does not appear that the CASE statement accepts || operators. See http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-case for more info. I did notice that the script ran perfectly except for the OR statements.

YuanHung Kuo
YuanHung Kuo
11,391 Points

Not sure if this question still confusing someone else like me. Just found a statement which may answer this question:

In a case statement, a "," is the equivalent of "||" in an if statement.

Hope this could give some help :)