From: ara.t.howard@... Date: 2006-10-25T23:04:22+09:00 Subject: Re: Reopening Classes On Wed, 25 Oct 2006, anthony.green@bbc.co.uk wrote: > Can someone offer an explaination as to what I'm encountering here.. > > ...................................................................................... > This code doesn't work... > > class Time > def <=>(other_time) > self.strftime("%H%M%S%Y%m%d").to_i <=> > other_time.strftime("%H%M%S%Y%m%d").to_i > end > end > > first = Time.local(2006, 10, 21) > last = Time.local(2006, 10, 27) > > time_slots = [] > range = first..last > range.step(1.hour) do |day_slot| > time_slots << day_slot > end > > time_slots.sort > > ...................................................................................... > > the time_slots array isn't populating properly > ...................................................................................... > > This does > > first = Time.local(2006, 10, 21) > last = Time.local(2006, 10, 27) > > time_slots = [] > range = first..last > range.step(1.hour) do |day_slot| > time_slots << day_slot > end > > class Time > def <=>(other_time) > self.strftime("%H%M%S%Y%m%d").to_i <=> > other_time.strftime("%H%M%S%Y%m%d").to_i > end > end > > time_slots.sort > > ...................................................................................... i see you already got an answer, but you might want to know about this idiom: harp:~ > cat a.rb lt = lambda{|*a| Time.local *a} hr = 3600 times = [] fields = %w( hour min sec year month day ) ( lt[2006, 10, 21] .. lt[2006, 10, 23] ).step(hr){|t| times << t} sorted = times.sort_by{|t| fields.map{|f| t.send f}} puts sorted.first(16) harp:~ > ruby a.rb Sat Oct 21 00:00:00 MDT 2006 Sun Oct 22 00:00:00 MDT 2006 Mon Oct 23 00:00:00 MDT 2006 Sat Oct 21 01:00:00 MDT 2006 Sun Oct 22 01:00:00 MDT 2006 Sat Oct 21 02:00:00 MDT 2006 Sun Oct 22 02:00:00 MDT 2006 Sat Oct 21 03:00:00 MDT 2006 Sun Oct 22 03:00:00 MDT 2006 Sat Oct 21 04:00:00 MDT 2006 Sun Oct 22 04:00:00 MDT 2006 Sat Oct 21 05:00:00 MDT 2006 Sun Oct 22 05:00:00 MDT 2006 Sat Oct 21 06:00:00 MDT 2006 Sun Oct 22 06:00:00 MDT 2006 Sat Oct 21 07:00:00 MDT 2006 the basic idea of fields = %w( one two three ) list.sort_by{|elem| fields.map{|f| elem.send f}} is powerful. regards. -a -- my religion is very simple. my religion is kindness. -- the dalai lama