tags:

views:

185

answers:

2

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 
+3  A: 

x.each_with_index { |v, i| puts "current index...#{i}" }

banister
`i` should be the second parameter to the block
Chubas
Chubas, yeah that always gets me, thanks
banister
+7  A: 
X.each_with_index do |item, index|
  puts "current_index: #{index}
end
Chubas