问题
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!
回答1:
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"
回答2:
I had the same question and found this answer which didn't really explain why this is confusing to those just learning it. After looking closer I found why it behaves the way it does.
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]
assert_equal true, hash[:one].object_id == hash[:two].object_id
end
Look at the assignment lines:
hash[:one] << "uno"
hash[:two] << "dos"
Each assignment is a reference to a non-existent key, which points to the default key, then using the append operator (<<) adds an element to the default key's array. Thus all calls to the default key's array are now ["uno", "dos"].
That is also why the assert comparing the id's of both calls (hash[:one] and hash[:two]) as equal is true; they both reference the default key.
来源:https://stackoverflow.com/questions/35833583/why-does-the-test-default-value-is-the-same-object-in-about-hashes-rb-of-ruby-ko