Date math without the dates

Hi all. Observe:

(Date.today..Date.today + 90).to_a.size # ~90

That operation is, unfortunately, slow. Well, when you're calling it
100+ times it's slow. :slight_smile:

Is there a way to get at the result of that operation (an integer) with
two date objects? e.g.

class AccountTransaction

  attr_accessor :posted

  def day_count(date)
    (posted..date).to_a.size
  end

end

Any ideas?

···

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

Ack, some amendments...

Is there _another_ way to get at the result of that operation (an
integer) besides the one I posted above?

And, I suppose a better title would have been "Faster date math."

···

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

irb(main):001:0> ((Date.today + 90) - Date.today).to_i
=> 90

Subtracting Dates from each other returns a Rational.

irb(main):002:0> (Date.today + 90) - Date.today
=> Rational(90, 1)

You can just coerce that into an integer. No intermediate Array
required.

HTH,
Chris

···

On Apr 13, 11:23 pm, Daniel Waite <rabbitb...@gmail.com> wrote:

Ack, some amendments...

Is there _another_ way to get at the result of that operation (an
integer) besides the one I posted above?

And, I suppose a better title would have been "Faster date math."

--
Posted viahttp://www.ruby-forum.com/.

Chris Shea wrote:

You can just coerce that into an integer. No intermediate Array
required.

Ah, sweet! Crazy, too: I _just_ tried subtracting dates and found that a
Rational was returned, but didn't know you could coerce it into an
integer. Thanks so much Chris! (And yes, it's _much_ faster!)

···

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