Why one and not the other?


#1

I just had a quick question. When I was doing this, it was not converting the string to a symbol.

puts "What movie do you want to delete?"
    title = gets.chomp
    if movies[title] == nil then
        puts "That movie isn't there! You can't delete it!"
    else
        movies.delete(title.to_sym)
    end

I had to change it to this:

puts "What movie do you want to delete?"
    title = gets.chomp.to_sym
    if movies[title] == nil then
        puts "That movie isn't there! You can't delete it!"
    else
        movies.delete(title)
    end

Can anyone explain why the second example works, but not the first?


#2

The first example is searching for title and deleting title.to_sym, which is not the same movie title.

    if movies[title.to_sym] == nil then
        puts "That movie isn't there! You can't delete it!"
    else
        movies.delete(title.to_sym)
    end

Does work.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.