问题描述:

I'm doing the ruby koans exercises and am a bit confused about why the answers are such in the test_default_value_is_the_same_object method exercises. Below is the code:

def test_default_value_is_the_same_object

hash = Hash.new([])

hash[:one] << "uno"

hash[:two] << "dos"

assert_equal ["uno", "dos"], hash[:one]

assert_equal ["uno", "dos"], hash[:two]

assert_equal ["uno", "dos"], hash[:three]

end

I'm not sure why no matter what the key is, the value is always "uno" and "dos"? I thought when the key is one, the returned value should be "uno"; when key is "two", the returned value should be "dos". Why no matter what the keys are, the value is always an array?

Thank you and I'm looking forward to your answer!

网友答案:
hash = Hash.new([])

Will instantiate a new array with [] (let's call it Harvey), then make a hash with Harvey as its default.

hash[:one]

doesn't exist, so you get Harvey. Harvey gets "uno" added to him, using the Array#<< operator (equivalent to harvey.push("one"))

hash[:two]

also doesn't exist, so you get Harvey again (who, remember, already contains "uno"). He now also gets "dos".

hash[:three]

returns Harvey, still with his "uno" and "dos".

If you wanted the code to behave like you think it should, with a different array in each key, you need to return a new array every time you want a default, not Harvey every single time:

hash = Hash.new { |h, k| h[k] = [] }

And if you just want the hash to have nothing to do with arrays, ignore Harvey, and use Hash#[]= instead of Array#<<:

hash = Hash.new()

hash[:one] = "uno"
hash[:two] = "dos"
相关阅读:
Top