.3 The case Statement


#1



Hello everyone. I am totally stuck, I don't understand what's wrong with my code...
I "undressed" it to its most basic form to try to find maybe a typing mistake or anything, but the message is still the same, and I don't understand what's wrong, and what I should do...
Please help !


https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/3?curriculum_id=5059f8619189a5000201fbcb


Error message :
(ruby):10: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ',' or ';' or '\n'
when "add" puts "added!"
^
(ruby):11: syntax error, unexpected keyword_else, expecting $end
else puts "error"
^


movies={
    the_big_L: 5,
    fight_club: 5
}

puts "Choose a movie"

choice = gets.chomp

case choice
when "add" puts "added!"
else puts "error"
end


#2

mrfdl.rb:10: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ',' or ';' or '\n'
when "add" puts "added!"
               ^

Look at where it's pointing and what it's saying.
puts was unexpected, instead it expected one of:
then , ; \n
where \n is a newline character

So puts is not allowed there, but it lists what is allowed, you can then look up what effect those would have there.

You've got documentation for case here:
http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-case

And here's for then, on the same page:
http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-then

And those also briefly describe what effect comma and semicolon have


#3

Thank you, I'm gonna look this up, though I don't get why I get this error since I think I respected the syntax that was given in the examples and in the "course"...
It just makes me confused that I copied/pasted solutions from the forum that work just fine and have the exact same syntax as mine (at least i'm pretty sure they do... i am ready to accept that maybe i just forgot one "," somewhere of course...)


#4

It finally worked with

case choice
when 'add'
  puts "Added!"
else puts "error"
end

But I don't understand why as " " and ' ' are the same to me in that case


#5

If you're saying that changing between ' and " makes a difference there, then no, it does not, and you really need to question how you came to that conclusion!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.