def largest_hash_key(hash) key = hash.sort{|a,b| a[1] <=> b[1]}.last puts key end hash = { "n" => 100,"m" => 100,"y" => 300,"d" => 200,"a" => 0 } largest_hash_key(hash)
在此代码中,“puts”打印最大的键和值e.x y300.那么,我如何修改代码以找到最大值并将其键入to_s变量?
h = {"n" => 100,"a" => 0} key_with_max_value = h.max_by { |k,v| v }[0] #=> "y"