Skip to Content
Learn
The Zen of Ruby
When and Then: The Case Statement

The if/else statement is powerful, but we can get bogged down in ifs and elsifs if we have a lot of conditions to check. Thankfully, Ruby provides us with a concise alternative: the case statement. The syntax looks like this:

case language when "JS" puts "Websites!" when "Python" puts "Science!" when "Ruby" puts "Web apps!" else puts "I don't know!" end

But you can fold it up like so:

case language when "JS" then puts "Websites!" when "Python" then puts "Science!" when "Ruby" then puts "Web apps!" else puts "I don't know!" end

Instructions

1.

We’ve added some code to prompt the user for input.

Add a case greeting statement (in either style!) to the editor. It should do the following:

  1. When the user types “English”, the program should puts "Hello!"
  2. When the user types “French”, the program should puts "Bonjour!"
  3. When the user types “German”, the program should puts "Guten Tag!"
  4. When the user types “Finnish”, the program should puts "Haloo!"
  5. Otherwise, the program should puts “I don’t know that language!”
Folder Icon

Take this course for free

Already have an account?