Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the test_default_value_is_the_same_object in about_hashes.rb of Ruby Koans exercises have the answer of an array?

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!

like image 254
Penny Avatar asked Mar 06 '16 22:03

Penny


1 Answers

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"
like image 137
Amadan Avatar answered Sep 30 '22 14:09

Amadan