Finding out current index in EACH loop (Ruby)

ming yeow picture ming yeow · May 17, 2010 · Viewed 105.9k times · Source

Possible Duplicate:
Automatic counter in Ruby for each?

I want to find out the current index while i am in the each loop. how do i do so?

X=[1,2,3]

X.each do |p|
 puts "current index..."
end 

Answer

Chubas picture Chubas · May 17, 2010
X.each_with_index do |item, index|
  puts "current_index: #{index}"
end