Skip to content

Instantly share code, notes, and snippets.

@chuck0523

chuck0523/max-by.rb

Created Apr 6, 2016
Embed
What would you like to do?
# 配列
a = ['banana', 'strawberry', 'apple']
# 一番文字列の長いものを返す
a.max_by { |x| x.length } # strawberry
# 引数に個数を指定できる。
a.max_by(2) { |x| x.length } # [strawberry, banana]
# ちなみに、評価式はもちらん以下の書式でもOK
a.max_by &:length # strawberry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment