How do I know what iteration I'm in when using the Integer.times method?

23,164

Yes, just have your block accept an argument:

some_value.times{ |index| puts index }
#=> 0
#=> 1
#=> 2
#=> ...

or

some_value.times do |index|
  puts index
end
#=> 0
#=> 1
#=> 2
#=> ...
Share:
23,164
EarlyPoster
Author by

EarlyPoster

Updated on November 17, 2020

Comments

  • EarlyPoster
    EarlyPoster over 3 years

    Let's say I have

    some_value = 23
    

    I use the Integer's times method to loop.

    Inside the iteration, is there an easy way, without keeping a counter, to see what iteration the loop is currently in?