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.
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?
This is more concise:
['abc', 'defg'].max_by{|x| x.length }
['abcd', 'def'].max_by &:length
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With