我有这个代码片段,在这种情况下,一个存储桶只是一个更大数组中的一个数组:
def Dict.get_slot(aDict, key, default=nil)
# Returns the index, key, and value of a slot found in a bucket.
bucket = Dict.get_bucket(aDict, key)
bucket.each_with_index do |kv, i|
k, v = kv
if key == k
return i, k, v
end
end
return -1, key, default
end
名为 k 和 v 的两个变量被设置为 kv 的内容。但是,当 kv 一次只包含一个值时,这怎么能工作呢?
我把它写到另一个文件中:
bucket = ['key', 'value']
key = 'key'
bucket.each_with_index do |kv, i|
k, v = kv
if key == k
puts k, v, i
end
end
然后 v 变量为空:
key
0
我的问题是,为什么在第一个示例中多重分配有效,而在第二个示例中无效?