这将返回数组中最长字符串的第一个实例:
["abcd","efgh","ijk"].max_by { |x| x.length } # => "abcd"
与此类似,是否有一种很好的方法来获取具有最大长度的所有字符串的数组?
["abcd","efgh","ijk"].some_trick ... # => ["abcd","efgh"]
开始了 :
["abcd","efgh","ijk"].group_by(&:size).max.last #=> ["abcd","efgh"]
解释 :
{4=>["abcd", "efgh"], 3=>["ijk"]}
[key, values]
=>[4, ["abcd", "efgh"]]
["abcd", "efgh"]