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?

🛴ループ処理まとめ🔁❹ ー .uptoメソッド / .downtoメソッド

Posted at

整数専用のイテレータである.uptoメソッドと.downtoメソッドについて、アウトプットする。

具体例による使い方と説明

1.upto(5) do |i|
  puts i
end

# 実行結果 =>1 2 3 4 5
  • 1から5まで、昇順でブロックを回す
    .downtoメソッドも同様

注意点

  • 整数専用のイテレータ

整数以外を昇順・降順に並べたい場合

  • 昇順:(a..b).each do |i|
  • 降順:(a..b).reverse_each do |i|

まとめてみて

  • 配列、整数、数値、文字列、、、どのループ処理を使えばいいか洗濯できるようになりたい。
  • コードを書いていて、エラーが出る原因の大きな一つが👆これ。
  • ちゃんと理解したい。
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?