【问题标题】:How to use max_by and with_index in Ruby?如何在 Ruby 中使用 max_by 和 with_index?
【发布时间】: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


    【解决方案1】:
    a.each_with_index.max_by { |x,i| x.length }.last
    

    【讨论】:

      【解决方案2】:

      找到它:a.each_with_index.max_by {|x, idx| x.length }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-24
        • 2018-01-31
        • 1970-01-01
        • 2017-05-29
        • 2018-05-07
        • 2014-06-13
        • 2021-03-26
        • 1970-01-01
        相关资源
        最近更新 更多