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 trialErik Nuber
20,629 PointsCase 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!"
else
puts "Hi #{name}!"
end
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}"
else
puts "Ok, maybe later"
end
2 Answers
Matt Coston
18,425 PointsI 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
11,391 PointsNot 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 :)