67
43

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_with_indexとeach.with_index

Last updated at Posted at 2018-07-31

each_with_indexの場合

array = ["Ruby", "PHP", "Python"]
array.each_with_index do |element, index|
  p "#{index}#{element}"
end

#以下の様に出力
0Ruby
1PHP
2Python

each.with_indexの場合

array = ["Ruby", "PHP", "Python"]
array.each.with_index(1) do |element, index|
  p "#{index}#{element}"
end

1Ruby
2PHP
3Python

indexを指定の数字から始めたいときに便利

参考

【Ruby】each_with_indexは知ってたけどeach.with_indexは知らなかった… - 訳も知らないで

67
43
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
67
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?