From: chris@... Date: 2014-11-12T15:37:53+00:00 Subject: [ruby-core:66230] [ruby-trunk - Feature #10498] Make `loop` yield a counter Issue #10498 has been updated by Chris Seaton. But doesn't this mean #loop will only run FIXNUM_MAX times? Rather than run infinitely as it currently does? That's a pretty big semantic change. Also, why not just overflow to Bignum? ---------------------------------------- Feature #10498: Make `loop` yield a counter https://bugs.ruby-lang.org/issues/10498#change-49917 * Author: Franck Verrot * Status: Open * Priority: Normal * Assignee: ruby-core * Category: core * Target version: current: 2.2.0 ---------------------------------------- # 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 `` object, so we would just do `.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) -- https://bugs.ruby-lang.org/