In this exersize we are to make a program to organize our favourite movies.
I am stuck here:
04.Add that movie/rating pair to the movies hash and puts a message indicating the pair was added. (No need for to_sym or to_i just yet!)
When looking at what others have done and back to the example on the first page I see " ’’
’movies[title.to_sym].nil?
’’’
this is totally new to me. How does this add the key and values into the hash?
when "add"
puts "movie title"
title = gets.chomp
if movies[title.to_sym].nil?
puts "what's the rating (type a raiting of 0 > 4)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}"
else "that movie has been added, its rating is #{movies[title.to_sym]"
end
end
this is a way of adding the keys and values to the movies hash: movies[title.to_sym] = rating.to_i`` how so? and how does this:#{movies[title.to_sym] ``` returns the “title” from the hash once we have converted it into a symbol…
If you follow the instruction it doesn’t aske you to add a if statement 01. Inside your when “add” block, remove the puts “Added!” statement. 02. In its place, prompt the user for a movie title. Save the result in a new variable called title. (*Your code already has an example of how to do this!)
when "add"
puts "Which movie do you want to add?"
title = gets.chomp
end
03. Next, prompt the user for the rating of the movie. Save that in a new variable called rating.
when "add"
puts "Which movie do you want to add?"
title = gets.chomp
puts "what's the rating (type a raiting of 0 > 4)"
rating = gets.chomp
end
04. Add that movie/rating pair to the movies hash and puts a message indicating the pair was added. (No need for to_sym or to_i just yet!)
when "add"
puts "Which movie do you want to add?"
title = gets.chomp
puts "what's the rating (type a raiting of 0 > 4)"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}"
end