There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
case choice
when “add”
puts “What movie would you like to add?”
title = gets.chomp
movies[title]
when “update”
puts “What is your rating for the selected movie?”
rating = gets.chomp
movies[title] = rating
Placing this code, above, adds the key/ hash pair. However, you will need to complete this key/ value syntax by converting them to a symbol(.to_sym) and integer(.to_i), respectively.
Hello, I am having the same problem. I am stuck in this step. Here is what my code looks like. It seems I am not adding the key/value pair to the movies hash. Not sure how to do it? Can anybody help me?thanks!
movies = {“good fellas”=> “5”}
puts “what’s your favorite movie?”
choice=gets.chomp
case choice
when “add”
puts “what’s the title of the movie?”
title=gets.chomp
puts"what’s the rating for this movie?"
rating=gets.chomp
when “update”
puts “Updated!”
when “display”
puts “Movies!”
when “delete”
puts “Deleted!”
else
puts “Error!”
end
Hi @kwdm5389 and welcome to the forums! This part of the lesson focuses on adding a movie and its corresponding rating to your movies hash. When we enter the title and rating of our movie, they are passed through this line of code movies[title.to_s] = rating which stores them in our movies hash as a key/value pair. What its really doing is adding the title of the movie to the hash as a string, indicated by the .to_s method, and having it store the rating you entered. So, in this case, the title is our key and the rating is our value. Hope this helps, let me know if you need more clarification!
What I ended up doing was removing the final puts statement (line 19), then running the code. This seemed to work and for some reason met the requirement of the exercise.
I then added the put statement back it and ran it again. It then worked, and printed out the final line.
Instruction:
Add that movie/rating pair to the movies hash.
and the solution is given
movies[title.to_s] = rating
I didn’t really get this code!
kindly help me out Rubyists!
ok upon further research I think i figured out whats going on…
ruby is taking your “[title.to_s]” and saying this is your key in the new key/value pair and pairing it up with your other input, the “rating” and storing it in the movies hash…
check out this page on stack. https://stackoverflow.com/questions/6863771/how-to-add-to-an-existing-hash-in-ruby/6864345
Not quite. It is a string, already. We’re converting it to a symbol.
You get now that it is a key which is paired up with a value. Symbols are very unique and cannot be duplicated. That makes them ideal for use as keys in a hash. I may be talking out my chimney, here… Ruby is better optimized for symbols than strings. It follows that a symbol is one thing, whereas a string is several characters. To be sure you have the right string you have to confirm all the characters. To be sure you have the right symbol is no issue at all. You either have it or you don’t.
Getting frustrated with this exercise. I don’t understand how to add a key, value to an existing hash
movies = {
'Inglourious Basterds' => '5'
}
puts "What would you like to do?"
choice = gets.chomp
case choice
when "add"
puts "Go ahead, add a movie."
title = gets.chomp
puts "Nice. What would you rate this movie from 1-5?"
rating = gets.chomp
movies[title, rating]
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
Can anyone advise? Whats even more frustrating is that the solution includes the .to_s method which the exercise mentions “isn’t needed yet”.
Thank you for taking the time to respond. Unfortunately, I’m still stuck after applying the suggested change. I’m still getting an error saying “It looks like you didn’t add to the movies hash”
movies = {
'Inglourious Basterds' => '5'
}
puts "What would you like to do?"
choice = gets.chomp
case choice
when "add"
puts "Go ahead, add a movie."
title = gets.chomp
puts "Nice. What would you rate this movie from 1-5?"
rating = gets.chomp
movies[title] = rating
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end