1
a = %w(albatross dog horse)
a.max_by {|x| x.length } #=> "albatross"
如何使用max_by.with_index
来获取最大值(在本例中为0)的索引?如何在Ruby中使用max_by和with_index?
红宝石1.9.3
a = %w(albatross dog horse)
a.max_by {|x| x.length } #=> "albatross"
如何使用max_by.with_index
来获取最大值(在本例中为0)的索引?如何在Ruby中使用max_by和with_index?
红宝石1.9.3
a.each_with_index.max_by { |x,i| x.length }.last
发现:a.each_with_index.max_by {|x, idx| x.length }