Ruby 1.9.3中的未定义方法错误

时间:2017-02-21 20:08:54

标签: ruby

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:在`'

0 个答案:

没有答案