Project

General

Profile

Actions

Feature #10498

open

Make `loop` yield a counter

Added by cesario (Franck Verrot) over 9 years ago. Updated about 6 years ago.

Status:
Open
Target version:
-
[ruby-core:66220]

Description

Problem

Teaching Ruby, we always end up with that type of construct

i = 0
loop do
  i += 1
  # do something with i....
  raise StopIteration if i ...
end

Solution

What I propose with this patch is making loop yield the iteration count:

loop do |i|
  # do something with i....
  raise StopIteration if i ...
end

i starts at 0 and stops at FIXNUM_MAX (there's no Float::Infinity equivalent for integers).

Alternate solution

Integer#times could work if we had an <Integer's infinity> object, so we would just do <Integer's Infinity>.times { |i| ... }.

Also, this is the very first patch I submit to Ruby, I might have done something horrible, feel free to tell me :-)


Files

0001-vm_eval.c-loop-now-yields-a-incremented-counter.patch (1.74 KB) 0001-vm_eval.c-loop-now-yields-a-incremented-counter.patch cesario (Franck Verrot), 11/12/2014 06:25 AM
0001-vm_eval.c-loop-now-yields-a-incremented-counter.patch (1.86 KB) 0001-vm_eval.c-loop-now-yields-a-incremented-counter.patch Updated patch (no more limited to FIXNUM_MAX iterations) cesario (Franck Verrot), 11/14/2014 10:27 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0