我正在通过RubyKoans的about_hashes.rb。 1个运动让我不解:Ruby中的默认哈希值(Rubykoans.com - > about_hashes.rb)
def test_default_value
hash1 = Hash.new
hash1[:one] = 1
assert_equal 1, hash1[:one] #ok
assert_equal nil, hash1[:two] #ok
hash2 = Hash.new("dos")
hash2[:one] = 1
assert_equal 1, hash2[:one] #ok
assert_equal "dos", hash2[:two] #hm?
end
我的猜测是,Hash.new(“DOS”),使“DOS”的所有不存在键默认回答。我对吗?
我编辑了我的问题,它应该是“dos”而不是零。 – 2012-02-08 14:45:44