我是学习Ruby的新手。在下面的代码中,我犯了一个错误,写了一个错误的单词' hash
'而不是movies
'。但我发现它也是正确的方式.WHY这发生了
有人可以帮我解释一下吗?
movies = {:Frozen => 9}
puts "input a movie title:"
title = gets.chomp
puts "the rating of the movie:"
rating = gets.chomp
if hash[title.to_i].nil? #here,i wrote 'hash' instead of 'movies'
movies[title.to_sym] = rating.to_i
puts "Added successfully!"
else
puts "The movie already exists."
end
这些是我的输入和运行结果:
为什么hash[title.to_i]
似乎不是零,仍然会发出' The movie already exists.
' ?
input a movie title:
Frozen
the rating of the movie:
4
The movie already exists.
答案 0 :(得分:0)
您正在调用hash
main
方法
p self # => main
p self.methods.sort #=> [:!, :!=, :! ... :hash,...]
p self.hash # => -1928951575263865998