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?

minメソッドの使い方

Posted at

基本的な使い方

配列から最小値またはn番目まで小さい数値を出力する 

[1, 2, 3].min  #=> 1
[1, 2, 3].min(2) #=> [1, 2]

便利な使い方

if a > b のような大小比較をせずとも1行でかける 

ミスコード 

分岐が多くて見づらい

new_value = increment + current_values[i]
    if new_value <= max_limits[i]
        current_values[i] += increment
    else
        current_values[i] = max_limits[i]
    end
end 

改良コード

シンプルな1行

current_values[i] = [current_values[i] + increment, max_limits[i]].min

0
0
0

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?