Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created April 6, 2016 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/8829cdac7dead0afd066b9d7a80aaac7 to your computer and use it in GitHub Desktop.
Save chuck0523/8829cdac7dead0afd066b9d7a80aaac7 to your computer and use it in GitHub Desktop.
# 配列
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