ruby

Index count method for iterators in ruby?


When moving over an iteration such as:

array.each do |row|
  puts "Current row count: " + row.current_row_index
  # do some more stuff
end

Is there a way to get the index of the current iteration / row? Obviously I can just throw in a counter, but I'm curious if there's a shortcut for an index function that shows it's current position.

Been digging through the available methods via pry, however I've not seen anything that seems to do this out of the box.


Solution

  • array.each_with_index do |row, index|
      puts index
    end