Detect a key and replace it's value in hash

Hi.
I'm new to ruby and to programming too.I have a task which i need to
complete.

I have problems with the update part of the code. I need to check if the
key given by the user exists in the hash and if yes i need to update the
rating of that movie. So far the code can't find the key and print's out
the "Key not found " string.

Any help will be appreciated.

Here is the code:

movies = { Godfather: 4,
Grudge: 4,
Star_Wars: 4,
Pulp_Fiction: 3}

puts "Please enter your input: "
choice = gets.chomp

case choice
when "add"
    puts "Please enter the title of the movie: "
    title = gets.to_sym
    puts "Please enter the rating of the movie: "
    rating = gets.to_i
    movies[title] = rating
    if movies[title] == nil
        movies[title] = rating
    else
        puts "The movie #{title} was successfully added."
    end

when "update"
    puts "Please enter the title of the movie :"
    title = gets.to_sym
    if movies.key?(title) == true
        puts "Please enter the new rating: "
        rating = gets.to_i
        movies[title] = rating
    else
        puts "Key not found!"
    end

···

--
Posted via http://www.ruby-forum.com/.

Maybe this gives you a clue:

2.0.0p195 :001 > gets.to_sym
a
=> :"a\n"

A solution:

2.0.0p195 :002 > gets.chomp.to_sym
a
=> :a

Some comments on your code:

Hi.
I'm new to ruby and to programming too.I have a task which i need to
complete.

I have problems with the update part of the code. I need to check if the
key given by the user exists in the hash and if yes i need to update the
rating of that movie. So far the code can't find the key and print's out
the "Key not found " string.

Any help will be appreciated.

Here is the code:

movies = { Godfather: 4,
Grudge: 4,
Star_Wars: 4,
Pulp_Fiction: 3}

puts "Please enter your input: "
choice = gets.chomp

case choice
when "add"
    puts "Please enter the title of the movie: "
    title = gets.to_sym
    puts "Please enter the rating of the movie: "
    rating = gets.to_i
    movies[title] = rating
    if movies[title] == nil
        movies[title] = rating
    else
        puts "The movie #{title} was successfully added."
    end

This is very strange. Why are you checking nil in the movies[title]
just to do the same assignment again?

when "update"
    puts "Please enter the title of the movie :"
    title = gets.to_sym
    if movies.key?(title) == true

no need to do == true.

        puts "Please enter the new rating: "
        rating = gets.to_i
        movies[title] = rating
    else
        puts "Key not found!"
    end

Hope this helps,

Jesus.

···

On Mon, Sep 9, 2013 at 11:04 AM, Red Rev <lists@ruby-forum.com> wrote:

PS: its not an good idea to use to_sym on user input on maybe longer
running objects, because this symbols are not deleted from the GC so you
may fill your RAM with it

···

--
Posted via http://www.ruby-forum.com/.

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1121014:

Maybe this gives you a clue:

2.0.0p195 :001 > gets.to_sym
a
=> :"a\n"

A solution:

2.0.0p195 :002 > gets.chomp.to_sym
a
=> :a

Some comments on your code:

        puts "The movie #{title} was successfully added."
    end

This is very strange. Why are you checking nil in the movies[title]
just to do the same assignment again?

when "update"
    puts "Please enter the title of the movie :"
    title = gets.to_sym
    if movies.key?(title) == true

no need to do == true.

        puts "Please enter the new rating: "
        rating = gets.to_i
        movies[title] = rating
    else
        puts "Key not found!"
    end

Hope this helps,

Jesus.

Thanks, it works now! Forgot to chomp.

The nil check is there to check if the code in that part is right. I was
not sure.

Thanks again. :wink:

···

On Mon, Sep 9, 2013 at 11:04 AM, Red Rev <lists@ruby-forum.com> wrote:

--
Posted via http://www.ruby-forum.com/\.