It won't update..?


#1

Hi everyone,

I'm a bit confused, i'm one the display exercice which basically allows me to print my whole hash in order to check it, here is my code :

movies = Hash.new
movies["Usual Suspect"] = 5

puts "Enter a command"
choice = gets.chomp

case choice
when "add"
puts "What's the movie that you want to add?"
title = gets.chomp
if movies[title].nil?
puts "Rate it !"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "Added!"
else
puts "Already added"
end
when "update"
puts "What do you want to update?"
title = gets.chomp
if movies[title].nil?
puts "We don't have that movie stored"
else
puts "Rate it again!"
rating = gets.chomp
movies[title] = rating
puts "New rate : #{rating}"

 end

when "display"
movies.each do |x, y|
puts "#{x}: #{y}"
end

when "delete"
puts "Deleted!"
else
puts "Error!"
end

Everything is doing fine but in order to test my display command this is what I did, I enter the update command to update my hash
Then I put an other rate to it
Then I enter the display commande to see if my new rate is effective and...it is not...
I believe that the probleme is in the update commande but I don't know why... and where...
Sorry for my english all and thanks in advance !


#2

The best way to check if your update command really works is simply to make it print the whole hash after the modification, because otherwise, every time you're going to restart the program you'll get the original hash (in your case with "Usual Suspect" => 5).


#3

Hi there,
I just added "print movies" after all of my add code and It shows the hash that has been successfully modified but when I type display it don't shows me the hash updated...


#4

Your display command will only be usable at the start of your program, and at the start of your program, your hash has only one movie (the one you stored manually).


#5

Thank you so much I feel so dumb right now! I DID IT YAY!

here is what I added in my case add
when "add"
puts "What's the movie that you want to add?"
title = gets.chomp
if movies[title].nil?
puts "Rate it !"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "Added!"
print movies
else
puts "Already added"
end
puts "do something else!"
choice = gets.chomp
case choice
when "display"
puts movies
end

but it seems that i must add all of my previous case method to adapt to the user in put
how can I lighten it ?


#6

I'm not sure what you mean by that.

What exactly do you want to adapt to the user input?


#7

Well If I restart the program and puts display it won't display what it shows me earlier, is it normal ? sorry for my english


#8

As I said, your program has an array described in it, so when you use "display", it will always display this array, and not the changes you made, because the changes you made were made in a previous program, not this one.

If you want to be able to "add" and then "display" your changes, you'll need to use a loop in your program, and add an "exit" command which will end the loop (and therefore terminate your program).