LoginSignup
66
43

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は知らなかった… - 訳も知らないで

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