Mega Code Archive

 
Categories / Ruby / Statement
 

If vs Case

command = "Stop" case command when "Go"   puts "Going" when "Wait"   puts "Waiting" when "Turn"   puts "Turning" when "Stop"   puts "Stopping" else   puts "I can't understand that." end # This example also could be written as an if statement with a collection of elsif and else clauses: command = "Stop" if command == "Go"   puts "Going" elsif command == "Wait"   puts "Waiting" elsif command == "Turn"   puts "Turning" elsif command == "Stop"   puts "Stopping" else   puts "I can't understand that." end