更简洁的 max/min 版本,没有块 [英] More concise version of max/min without the block
本文介绍了更简洁的 max/min 版本,没有块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通常做 ['abc', 'defg'].max{|a, b|a.长度<=>b.length}
,但这似乎需要很多额外的输入来比较两个对象上相同方法的结果.
I normally do ['abc', 'defg'].max{|a, b| a.length <=> b.length}
, but this seems like a lot of extra typing to compare the results of the same method on both objects.
有没有更简洁的方法来做类似 ['abc', 'defg'].max(:length)
的事情,它会在每个对象上运行一个给定的方法进行比较?
Is there a more concise way, to do something like ['abc', 'defg'].max(:length)
, which would run a given method on each object for the comparison?
推荐答案
['abcd', 'def'].max_by &:length
这篇关于更简洁的 max/min 版本,没有块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文