Why does the second assert_equal below work? How does ruby associate “dos” with :two?
def test_default_value
hash2 = Hash.new("dos")
hash2[:one] = 1
assert_equal 1, hash2[:one]
assert_equal "dos", hash2[:two]
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The argument to
Hash.newis the “default” value – when a key isn’t present, and is read, return that value instead. You can also pass a block toHash.newto have more complex behaviour around default values.http://ruby-doc.org/core-1.8.7/Hash.html#method-c-new