Stuck on Display (1.8)


Hey, I've gone over my code a million times and know that it's something very small that I'm missing. The error message i'm given is that "It looks like your 'puts' doesn't include Step Brothers: 5"...although it prints the movies hash on the console (but with default values of 4?) I even tried running the code in a different editor and although there was no error message, my movie titles printed to the console, but not my values. Here is my exact code:

movies = {"Step Brothers" =>5,
"The Matrix" =>5,
"Ice Age" =>4, 
"Hackers" =>2}
puts "What would you like to do?"
choice = gets.chomp
case choice
when "add"
puts "What's the title?"
title = gets.chomp
if movies[title.to_sym].nil? 
puts "What's the rating? (0-5)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i 
puts "Movie has been added."
    puts "Movie already exists in database!"
when "update"
puts "Please enter new movie title."
title = gets.chomp
if movies[title.to_sym].nil?
    puts "This movie is not in the database."
    puts "Please rate your new movie."
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
when "display"
movies.each do |movies, ratings| 
puts "#{movies}: #{rating}"
when "delete"
puts "Deleted!"
    puts "Error!"

If anyone would like to help save a few hair follicles from being pulled out that would be awesome!


Try writing this,

movies.each do |movie, rating|
    puts "#{movie}: #{rating}"


Thanks!! Even though the "movies" keyword was fine, it did lead me to finding an unwanted "s" at the end of my rating keyword. :expressionless:
The devil is truly in the details lol.


though not semantically pure. We don't pull a 'books' from the shelf, but a 'book'.

books.each { |book, author| puts "#{book} by #{author}" }


books = {
    "Gulliver's Travels" => "Jonathan Swift",
    "Prester John" => "John Buchan",
    "The Light in the Forest" => "Conrad Richter",
    "The Snow Walker" => "Farley Mowat",
    "Etcetera" => "This Is Your Selection"


"Lightbulb" Thanks for that explanation. Definitely simplified the syntax for me. I was assuming the keywords had to be the same as the hash title or variable names. So keywords are just to serve as placeholders? If i used |m, r| puts "#{m}: #{r}" , theoretically it should return the same output?


On the same object, yes. The only thing that is set is the name of the object. books or movies. We cannot change the name of a hash.

Also note that,

|book, author|

is | key, value | for a hash, and | index, value | for an array.

object[key] == value

object[index] == value

With the object in the plural, and the member element in the singular we can see how the mapping works. It's an in the head thing.


Thanks again! Definitely made things a whole lot easier. Literally noted lol.


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