在Ruby Koans中,关于哈希的部分(about_hashes.rb)包括以下代码和注释:
def test_changing_hashes
hash = { :one => "uno", :two => "dos" }
hash[:one] = "eins"
expected = { :one => "eins", :two => "dos" }
assert_equal true, expected == hash
# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
end
我无法回答评论中的奖励问题 - 我尝试实际执行他们建议的替换,结果是一样的。我只能理解它是为了可读性,但我在本教程的其他地方没有看到类似的编程建议。
(我知道这听起来像某个已经得到权威回答的问题,但我找不到任何权威的资料。)
assert_equal true,{:one =>“eins”,:two =>“dos”} == hash
- skrobulassert_equal ({:one =>“eins”,:two =>“dos”}),hash
。 - user3054986