0
0

More than 3 years have passed since last update.

[Ruby]Integer#downto

Posted at

はじめに

最近コメントいただいた中で新しいメソッドを教えて頂いたので忘れないうちに記事にしておきます。

Integer#downto

integer型の数字に使えます。
使用した数字から1ずつ減らした数を一つずつブロック変数に入れて繰り返し処理をします。

引数にどこまで減らすか、最小の数字を渡すことで、最小値を指定できます。

6.downto(0) do |i|
  print i
end
=> 6543210


6.downto(1) do |i|
  print i
end
=>654321


6.downto(3) do |i|
  print i
end
=>6543

参考記事では下記のような使い方もしていました。
そのまま引用します↓

5.downto(1) {|i| print i, " " } # => 5 4 3 2 1

参考: https://docs.ruby-lang.org/ja/latest/method/Integer/i/downto.html

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