我有以下数组:
arr = [1,2,"car"]
arr.each do |e|
puts e
end
#=> 1
2
car
但是如何制作一个也显示其索引号的“put e”?
arr = [1,2,"car"]
arr.each_with_index() do |e,i|
puts "#{e} is at index #{i}"
end
# >> 1 is at index 0
# >> 2 is at index 1
# >> car is at index 2
arr = [1,2,"car"]
arr.each_with_index do |e, index|
puts index, e
end
仅获取索引:
arr = [1,2,"car"]
arr.each_index do |index|
puts index
end