ruby - 如何在 Ruby 中使用 max_by 和 with_index?

标签 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

最佳答案

a.each_with_index.max_by { |x,i| x.length }.last

关于ruby - 如何在 Ruby 中使用 max_by 和 with_index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11183537/

相关文章:

python - 查找最大和最小日期

javascript - 从数组的总长度中减去并得到一个干净的数字 [Javascript]

ruby - 在 Ruby 中链接流函数的最干净的方法是什么?

ruby-on-rails - 更新 crontab (Whenever gem)

iphone - NSString 对象的最大长度是多少?

ruby-on-rails-3 - ruby_threadptr_data_type 错误

ruby - pack() 和 unpack() 在 Ruby 中如何工作

ruby - Ruby 1.9.3-p392+ 中的 SSL 连接错误

ruby - 使用 rvm 在没有 pthread 的情况下安装 ruby

javascript - 如何验证单选按钮?