I have an Array of Events and I want to divide them into a 2-dimensional Array based on event_date (which returns a Time object). But I want it to be by the day part of the date. Here's what I have so far:
def divide_events_by_day(events)
# Sort, so we can start with the first day.
events = events.sort { |x,y| x.event_date <=> y.event_date }
days = []
current_date = events[0].event_date # A Time object.
# With meaningful hours and minutes.
while events.length > 0 do
# Find events for the current date.
day_events = events.select { |e|
(e.event_date.year == current_date.year) &&
(e.event_date.yday == current_date.yday)
}
# Move them to their subarray.
days << day_events
events = events - day_events
# Advance the current_date by one day.
current_date = (current_date + (60 * 60 * 24))
end
days
end
Any suggestions to make it simpler or faster?