On Wed, 28 Feb 2007, Peter Bailey wrote:

> me I could do it with less code. If I create a range between that last
> day of the month and all 7 days prior to it, that last Sunday should be
> in there somewhere. When I try to work with ranges, though, and ask for
> any results, it returns results for literally every second of every day
> in that range! So, can anyone help me to ask Ruby to just give me days
> back and not any subdivisions of those days?
>
> t1 = Time.mktime(2007,1,24)
> t2 = Time.mktime(2007,1,31)
> dates = t1 .. t2
> dates.each for |date|
>  ...                        #puts "date is a Sunday."
>  end
> end

Do it with Date instead of Time.

require 'date'
t1 = Date.new(2007,1,24)
t2 = Date.new(2007,1,31)
dates = t1 .. t2
dates.each do |date|
   # your date stuff here
end


Time uses seconds to represents a point in time.  Time is fast, because 
it's really just a thin layer between Ruby and the underlying C libraries.

Date and DateTime use days for their unit, represented by rational numbers 
(instances of the Rational class).  They are a lot slower than Time 
objects, but have some flexibility that Time doesn't, and the use of Days 
for their unit means they work well for your application.

If you want to convert a Date into a Time, do this:

Time.local(date.year,date.month,date.day)


Kirk Haines