◆ In the case of each_with_index
array = ["Ruby", "PHP", "Python"]
array.each_with_index do |element, index|
p "#{index}:#{element}"
end
#The output is as follows
# 0:Ruby
# 1:PHP
# 2:Python
◆ In the case of each.with_index
array = ["Ruby", "PHP", "Python"]
array.each.with_index(1) do |element, index|
p "#{index}:#{element}"
end
# 1:Ruby
# 2:PHP
# 3:Python
Until now, using each_with_index, I purposely displayed it as "index + 1" from 1 Each.with_index is convenient because you can start with a specified number other than 1!
Recommended Posts