我所經歷的Ruby Koans tutorial series,當我在about_hashes.rb
臨到這一點:shovel(<<)運算符在Ruby Hashes中如何工作?
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
值在assert_equals
,實際上是預期的教程。但我不明白如何使用<<
運營商和=
運營商有什麼區別?
我的期望是:
hash[:one]
將["uno"]
hash[:two]
將["dos"]
hash[:three]
將[]
是否有人可以解釋爲什麼我的期望是錯誤的?
有趣,這正是我的預期。然後,山再次只是山。 – 2013-03-03 07:19:17