我正在编写一个 TFIDF 程序 - 所有这些都应该没问题,但是我在哈希按预期工作时遇到了一个小(或大..)问题。
为了简短起见,手头的代码是:
#Word matrix is an array that contains hashes (obviously)
#i've done some stuff before this and these are working as expected
puts word_matrix[3][:yahoo] # => 2
puts word_matrix[100][:yahoo] # => 0
puts $total_words_hash[:yahoo] #=> 0
#Essentially, this block is taking a hash of all the words (values = 0) and trying
#to run through them adding the only the values of the other hash to the temporary
#and then setting the temp to the old hash position (so that there are 0 values
#and the values occurring in that document.. yet, it assigns the same values to
#ALL of the hashes of word_matrix[]
#now we run this block and everything breaks down for some reason..
for i in 0...word_matrix.size
tmp_complete_words_hash = $total_words_hash #all values should be zero...
word_matrix[i].each do |key,val| #for each key in the hash we do this..
tmp_complete_words_hash[key] = val
end
word_matrix[i] = tmp_complete_words_hash
end
puts word_matrix[3][:yahoo] # => 2
puts word_matrix[100][:yahoo] # => 2 -- THIS SHOULD BE 0 Still...
谁能解释为什么要为数组的所有哈希分配相同的值?好像tmp_complete_words_hash
不是每次都被重置。