10, Delete - works without ! for the delete, are hash methods by reference?


#1

for the delete branch of this exercise, I get a syntax error if I include ! at the end of the delete method call

when "delete"
puts "Enter the title you wish to delete"
title = gets.chomp
if !movies[title.to_sym]
puts "#{title} is not in the list, so we can't delete it"
else
puts "#{title} deleted"
movies.delete(title.to_sym)!
end

but this works:

when "delete"
puts "Enter the title you wish to delete"
title = gets.chomp
if !movies[title.to_sym]
puts "#{title} is not in the list, so we can't delete it"
else
puts "#{title} deleted"
movies.delete(title.to_sym)
end

I've printed out the hash after the working version and it is empty.

any ideas? the instructions say to use ! which from my limited understanding modifies the variable inline instead of returning a copy of the modified variable, is this different for hashes?


#2

Not sure this is correct. I can find no documentation on such a statement. You could try,

if movies[title.to_sym].nil?

Likewise I could not find documentation for this statement, either. I think you can safely leave off the !.

  if movies[title.to_sym].nil?
    puts "That movie was not found."
  else
    movies.delete(title)
    puts "#{title} deleted!"
  end