26
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

配列をインデックス付きで逆順にeachする

Posted at

ただ単に逆順にしたいならArray#reverseを使えばいいが、それにwith_indexを使うと昇順になってしまう。なので、each_with_indexに、reverse_eachを使う。

%w(one two three).each_with_index.reverse_each do |v, i|
    puts "#{v} : #{i}"
end
three : 2
two : 1
one : 0
26
18
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
26
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?