Method: Kernel#loop_with_index

Defined in:
lib/mug/loop-with.rb

#loop_with_index(offset = 0) ⇒ Object

Repeatedly executes the block, yielding the current iteration count, which starts from offset. If no block is given, returns an Enumerator.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mug/loop-with.rb', line 8

def loop_with_index(offset=0)
  return enum_for(:loop_with_index, offset) unless block_given?
  c = 0 + offset
  begin
    while true
      yield c
      c += 1
    end
  rescue StopIteration
  end
  c
end