03/10 The Case Statement


#1



movies = {
    "Interstellar" => 4.5,
    "Star wars" => 4
}

puts "Welcome to the Movie Rater!"
puts "What would you like to do?."
puts "--Add a movie"
puts "--Update a movie"
puts "--Display all movies"
puts "--Delete a movie"

choice = gets.chomp

case choice
    when "add" || "Add"
        puts "Added!"
    when "update" || "Update"
        puts "Updated!"
    when "display" || "Display"
        puts "Movies!"
    when "delete" || "Delete"
        puts "Deleted!"
else
    puts "Error!"
end

I know that I can use the .downcase! method in order to downcase the input given by the end user but I would like to know why it wont work like i have it listed above.


#2

This is a boolean expression. and does not evaluate to a string.

case choice.downcase

is the way to go.

Something to try, for the fun of it...

case true
  when choice == 'add' || choice == 'Add'

but it obviously is not very compact.


#3

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