Re: Duration

Peter Vandenabeele
Tue, 21 Dec 2010 22:07:29 -0000

Frederick Cheung wrote in post #969871:
> On Dec 21, 5:17pm, Jack Waugh
<7aq3z2h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>> It looks as though if you ask a DateTime to add something to itself,
>> it looks at the something, and if it''s an integer, it
interprets it as
>> a duration in seconds, but if it''s a Rational, it interprets
it as a
>> duration in days.
>
> That''s not actually the whole store - If you did DateTime.now + 1
then
> you would get 1 day after now.
> The key is that 1.second (and similarly 3.days, 2.hours + 6.minutes
> etc) aren''t integers - they''re ActiveSupport::Duration
objects.
> They quite often pretend to be integers (the number of seconds
> represented by the duration - although this is obviously dodgy if the
> duration is something like 1 month), largely because that''s quite
> convenient when dealing with Time objects and I suspect because of
> backwards compatibility (at one point in Rails'' history
> the .second/.day... methods were just convenience methods that
> returned the corresponding number of seconds)

I am actually curious how I could see from the object
itself in a Rails context that it''s not "really" a Fixnum,
but an ActiveSupport::Duration object

$ rails c
Loading development environment (Rails 3.0.3)
001:0> weekend = 2.days
=> 2 days
002:0> weekend.class
=> Fixnum   # but there is more functionality than a "Fixnum"
003:0> weekend.singleton_class
=> #<Class:#<ActiveSupport::Duration:0xb727e068>> # a hint ?

Also, here I did use specific units (in-casu "days"),
but still I had naively expected a different result
on line 005.

004:0> weekend.seconds
=> 172800 seconds # this is correct
005:0> weekend.days
=> 172800 days # I had expected ''2'' or ''2
days''

Thanks,

Peter

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org