0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】配列の最大値をとる

Posted at

例えば

.rb
numbers = [4,50,19,-12,0]

この配列から最大値をとるとき

.rb
max = numbers[0]
numbers.each do |number|
  max = number if number > max
end
puts max
# => 50

みたいな面倒なことをしていたのですが、sortメソッドなるものを知ったので

.rb
puts numbers.sort[numbers.length-1]
# sortで配列を小さい順に並び替えて、最後の値を取得する

とても楽になりました。

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?