1
0

More than 3 years have passed since last update.

Rubyの繰り返しstepについて

Posted at

Rubyの学習においてstepというものを学習したので
備忘録を兼ねて書いておきます。

nからmまで数値をxずつ増やしながら何らかの処理を実行したいときに利用します。

構文は
開始式.step(上限式,一度に増減する大きさ)
のように書きます。


1から10まで2ずつ増やしたい時

1.step(10, 2) { |n| puts n}

出力結果

1
3
5
7
9

10から1まで2ずつ減らしたい時

10.step(1, -2) { |n| puts n}

出力結果

10
8
6
4
2

1
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
1
0