LoginSignup
0
0

More than 1 year has passed since last update.

each.with_indexメソッド(Ruby)

Last updated at Posted at 2022-01-15

Ruby3.1
Enumeratorクラスwith_index
Enumerableモジュールeach_with_index
要素にインデックスを添えて繰り返す

a = ["a","b","c"]
a.each.with_index(2) do |s, i|
  puts "#{i}:#{s}"
end

2:a
3:b
4:c
#each.with_index(引数)とすることで、引数を受け取る。
#each_with_indexは引数を受け取らないため、添字は0から始まる
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