Forum: Ruby Detect a key and replace its value in hash.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0c3a4821cbc2689a7a9f19340b3f34e1?d=identicon&s=25 Red Rev (redrev)
on 2013-09-09 11:04
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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-09-09 11:39
(Received via mailing list)
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:

On Mon, Sep 9, 2013 at 11:04 AM, Red Rev <lists@ruby-forum.com> wrote:
>
>
>         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.
0c3a4821cbc2689a7a9f19340b3f34e1?d=identicon&s=25 Red Rev (redrev)
on 2013-09-09 11:50
"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:
>
> On Mon, Sep 9, 2013 at 11:04 AM, Red Rev <lists@ruby-forum.com> wrote:
>>
>>
>>         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. ;)
D9ebdcb66f1583378e6f72155db507e2?d=identicon&s=25 Hans Mackowiak (hanmac)
on 2013-09-09 20:35
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
This topic is locked and can not be replied to.