require "test/unit/assertions"
include Test::Unit::Assertions
def key_for_min_value(hash)
arr = Array.new
arr = hash.min_by{ |key,value| value}
if arr[0]==nil
return nil
else return arr[0]
end
end
assert_equal key_for_min_value({"k" => 2, "h" => 3, "j" => 1}), "j"
assert_equal key_for_min_value({"o" => 0, "z" => -2, "j" => 1}), "z"
assert_equal key_for_min_value({}), nil
此代码返回以下错误。为什么?
test1.rb:7:在key_for_min_value': undefined method
[]' for nil:NilClass(NoMethodError)
来自test1.rb:13:在`'