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

🛴ループ処理まとめ🔁❺ ー .stepメソッド

Posted at

ステップ=🏃(こんなイメージ)
文字通り.stepするメソッドである.stepメソッドについて学んだことをアウトプットする。

.stepメソッドとは

  • 開始値から増分ずつ足していき、値が上限値を超えない範囲でブロックを繰り返すメソッド
  • 🏃 ←こんなイメージ

記法と具体例

Range版:(開始値..上限値).step(増分) do |i|

(1..9).step(2) do |i|
  puts i
end

# 実行結果 => 1, 3, 5, 7, 9

Numeric版:`開始値.step(上限値, 増分) do |i|

1.step(9, 2) do |i|
  puts i
end

# 実行結果 => 1, 3, 5, 7, 9
  • Range :範囲
  • Numeric:数値

まとめてみて

  • 個人的に、Range版の方が見やすくて好き。
  • 例で言うと、「1から9まで、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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?