Here’s my problem:
I have an array of shipments, each of them has a ship_date
@shipments = Shipment.find(:all, :order => ['ship_date ASC'])
I have an array of dates for which I will display scheduled shipments:
@date_range = (start_date..end_date).to_a
I am trying to create a hash that uses each scheduled date as the key
and whose value is an array of shipments scheduled for that date.
I get the array of scheduled ship dates like this:
valid_dates = @shipments.collect {|s| s.ship_date}.uniq!
Then proceed to build my nested hash like this:
@ship_hash = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
valid_dates.each do |date|
@ship_hash[date][@shipments.select {|event| event.ship_date ==
date}]
end
What this @ship_hash looks like is this:
{Tue, 29 Jul 2008=>{[#<Shipment_1>, #<Shipment_2>]=>{}, Thur, 31
Jul 2008=>{[#<Shipment_3>]=>{}}}
My code is inserting a blank hash at the end of each value… I don’t
understand Ruby enough to find it after squinting and Googling for
some time. What I’d like it to look like is:
{Tue, 29 Jul 2008=>[#<Shipment_1>, #<Shipment_2>], Thur, 31 Jul
2008=>[#<Shipment_3>]}
Any help is much appreciated… thanks!
SH