From: "Ara.T.Howard" Date: 2003-11-27T09:22:12+09:00 Subject: Re: Time#succ ? On Thu, 27 Nov 2003, Paul Brannan wrote: > Date: Thu, 27 Nov 2003 06:29:10 +0900 > From: Paul Brannan > Newsgroups: comp.lang.ruby > Subject: Re: Time#succ ? > > On Mon, Nov 24, 2003 at 05:36:49AM +0900, Hal Fulton wrote: > > Should this be added? It would enable the creation > > of Time ranges like t1..t2 and so on. > > Does this not already work? > > [pbrannan@zaphod ruby]$ irb > irb(main):001:0> t = Time.now > => Wed Nov 26 16:27:46 EST 2003 > irb(main):002:0> (t .. t+1) > => Wed Nov 26 16:27:46 EST 2003..Wed Nov 26 16:27:47 EST 2003 > > (the problem only comes if you want to iterate over the range, which > seems to me to be an ill-defined operation). anyone familiar with the 'jot' unix program? i think ranges should work like that - eg. one should be able to define the increment day = 60 * 60 * 24 a = Time.now b = Time.now + (7 * day) week = (a...b) week.each(step = day) do |weekday| ... end as it is needs to overide the #succ method of an object for that to occur. i realize this is problematic since currently ranges only rely on having a #succ method, but perhaps this could be extend such that Range#each takes an optional argument, step. if this is given it will be passed to the objects #step method, something similar to: ~ > cat timestep.rb class Range alias __each each def each step = nil, &block nxt, e = self.begin, self.end if nxt.respond_to? :step loop do yield nxt nxt = nxt.step step break if (exclude_end? ? nxt >= e : nxt > e) end else __each &block end end end class Time; def step n; self + n; end end a = Time.now b = Time.now + (7 * 24 * 60 * 60) week = (a...b) week.each(24 * 60 * 60) do |day| p day end ~ > ruby timestep.rb Wed Nov 26 15:29:18 MST 2003 Thu Nov 27 15:29:18 MST 2003 Fri Nov 28 15:29:18 MST 2003 Sat Nov 29 15:29:18 MST 2003 Sun Nov 30 15:29:18 MST 2003 Mon Dec 01 15:29:18 MST 2003 Tue Dec 02 15:29:18 MST 2003 Wed Dec 03 15:29:18 MST 2003 -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================