Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Time#succ ?

Reply
Thread Tools

Time#succ ?

 
 
Hal Fulton
Guest
Posts: n/a
 
      11-23-2003
Should this be added? It would enable the creation
of Time ranges like t1..t2 and so on.

I'd assume that seconds would be the default resolution
(although the actual resolution is platform-dependent).

Hal



 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      11-24-2003
Hi,

In message "Time#succ ?"
on 03/11/24, Hal Fulton <> writes:

|Should this be added? It would enable the creation
|of Time ranges like t1..t2 and so on.
|
|I'd assume that seconds would be the default resolution
|(although the actual resolution is platform-dependent).

I like the idea. How should I treat sub-second value?

n = Time.now
p n.to_f # 1069633904.88943
p n.succ.to_f # 1069633905.88943 or 1069633905.0 or 1069633906.0?

matz.


 
Reply With Quote
 
 
 
 
Hal Fulton
Guest
Posts: n/a
 
      11-24-2003
Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Time#succ ?"
> on 03/11/24, Hal Fulton <> writes:
>
> |Should this be added? It would enable the creation
> |of Time ranges like t1..t2 and so on.
> |
> |I'd assume that seconds would be the default resolution
> |(although the actual resolution is platform-dependent).
>
> I like the idea. How should I treat sub-second value?
>
> n = Time.now
> p n.to_f # 1069633904.88943
> p n.succ.to_f # 1069633905.88943 or 1069633905.0 or 1069633906.0?


I would expect the first case.

Then n.succ.to_f would behave the same as (n+1).to_f,
isn't that correct?

Hal




 
Reply With Quote
 
Paul Brannan
Guest
Posts: n/a
 
      11-26-2003
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).

Paul


 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      11-26-2003
Paul Brannan wrote:
> 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).


Hmmm. For some reason I thought that to have a Range, the
endpoint objects had to know #succ.

Apparently Time ranges do work fine. I don't know why I
never thought of them before.

As for iterating: I agree it's ill-defined, but I think it's
reasonable to let t.succ be the same as t+1 (since the latter
is already meaningful).

But since I can construct a Time range and call include? on it,
I suppose that's all we really need.

Hal



 
Reply With Quote
 
Ara.T.Howard
Guest
Posts: n/a
 
      11-26-2003
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'
================================================== =============================

 
Reply With Quote
 
Sabby and Tabby
Guest
Posts: n/a
 
      11-27-2003
"Ara.T.Howard" <> wrote:

> 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


Maybe Range#step:

day = 60*60*24
a = Time.now
b = a + 7 * day

(a..b).step(day) {|t| puts t}

Unfortunately:

./gem:20:in `step': cannot iterate from Time (TypeError)

A work-around:

a = Time.now.to_i
b = a + 7 * day

(a..b).step(day) {|t| puts Time.at(t)}
 
Reply With Quote
 
Niklas Frykholm
Guest
Posts: n/a
 
      11-28-2003
>>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


I think a rather nice way of doing this is "by example":

def loop_over(v1,v2,vn)
x = v1
step = v2 - v1
while x <= vn
yield x
x += step
end
end

loop_over(1,2,10) {|x| puts x} # -> 1,2,3,4,5,6,7,8,9,10

day = 60 * 60 * 24
t = Time.now

loop_over(t, t + day, t + 7*day) do |weekday|

// Niklas

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57