From: noreply@... Date: 2007-03-23T20:28:09+09:00 Subject: [ ruby-Bugs-9490 ] Date module, step method, infinite loop if +step+ is 0 should raise an exception? Bugs item #9490, was opened at 2007-03-23 11:28 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1698&aid=9490&group_id=426 Category: Standard Library Group: 1.8.5 Status: Open Resolution: None Priority: 3 Submitted By: Colin Bartlett (colinb2) Assigned to: Nobody (None) Summary: Date module, step method, infinite loop if +step+ is 0 should raise an exception? Initial Comment: The Date step method goes into an infinite loop if +step+ is 0, but the Integer step method raises an exception if +step+ is 0. This seems inconsistent, and I prefer the Integer step behaviour, but I can be convinced that it's a desirable inconsistency. I'm using the standard Ruby Installer for Windows Ruby Version 1.8.5, Installer Version 185-21 ruby --version -> ruby 1.8.5 (2006-08-25) [i386-mswin32] code of the step method in Tadayoshi Funaba's Date module # Step the current date forward +step+ days at a # time (or backward, if +step+ is negative) until # we reach +limit+ (inclusive), yielding the resultant # date at each step. def step(limit, step) # :yield: date da = self op = [:-,:<=,:>=][step<=>0] while da.__send__(op, limit) yield da da += step end self end # The following is code to show the different behaviour # of Integer step and Date step when +step+ is 0. $\ = " " ; $, = " " # helps print output legibly on single lines puts "\n ** Integer step example: exception raised if step = 0" 500.step( 505, 2 ) { | nxt | print nxt } # -> 500 502 504 50.step( 45, -2 ) { | nxt | print nxt } # -> 50 48 46 begin 5.step( 5, 0 ) { | nxt | puts nxt } rescue puts ' * exception raised: message = ' + $!.message end # -> * exception raised: message = step can't be 0 require 'date' c = Date.new( 2008, 1, 31 ) puts "\n\n ** Date step example:" + " no error if step = 0, but an infinite loop?" c.step( c + 3, 2 ) { | nxt | print nxt } # -> 2008-01-31 2008-02-02 c.step( c - 3, -2 ) { | nxt | print nxt } # -> 2008-01-31 2008-01-29 puts count = 0 c.step( c, 0 ) { | nxt | puts count.to_s + ": " + nxt.to_s count += 1 break if count > 10 } # -> "infinite" loop of 2008-01-31 ? ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1698&aid=9490&group_id=426