Get the number of times in a loop over a Hash object

I have an object of type Hash that I want to execute with hash.each do |key, value| . I would like to get the number of times I went through a loop starting at 1.

Is there a method similar to each that provides this (while still containing the hash key / value data), or do I need to create another counter variable to increment in the loop?

+4
source share
1 answer

Use each_with_index instead of each . Note: the index starts at 0:

 hash.each_with_index do |(key, value), index| 
+11
source

Source: https://habr.com/ru/post/1306372/


All Articles