【发布时间】:2012-06-25 04:30:17
【问题描述】:
a = %w(albatross dog horse)
a.max_by {|x| x.length } #=> "albatross"
如何使用max_by.with_index获取最大值的索引(本例为0)?
Ruby 1.9.3
【问题讨论】:
标签: ruby max ruby-1.9.3
a = %w(albatross dog horse)
a.max_by {|x| x.length } #=> "albatross"
如何使用max_by.with_index获取最大值的索引(本例为0)?
Ruby 1.9.3
【问题讨论】:
标签: ruby max ruby-1.9.3
a.each_with_index.max_by { |x,i| x.length }.last
【讨论】:
找到它:a.each_with_index.max_by {|x, idx| x.length }
【讨论】: