当我遇到这个时,我正在阅读Ruby Koans教程系列about_hashes.rb:
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
Run Code Online (Sandbox Code Playgroud)
其中的值assert_equals实际上是教程所期望的.但我无法理解使用<<运算符和=运算符之间有何区别?
我的期望是:
hash[:one] 将会 ["uno"]hash[:two] 将会 ["dos"]hash[:three] 将会 []有人可以解释为什么我的期望是错的吗?