difficulty nesting array in hash

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

I get the array of scheduled ship dates like this:

   valid_dates = @shipments.collect {|s| s.ship_date}.uniq!

Warning: uniq! will not always return the uniqued array (since it does
the modification in place):
[1,2,3].uniq! #=> nil
[1,2,3,3].uniq! #=> [1,2,3]

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

Isn't there an assignment missing here? Your hash default proc means
that saying @ship_hash[date] will create a new empty hash if needed,
but then did you mean

@ship_hash[date] = @shipments.select {|event| event.ship_date ==date}]

Fred

Thanks for the reply, Fred.

I'll add a condition to check if there are no duplicates in the array
to avoid the uniq! gotcha.

The assignment you suggested worked like a peach. Thanks!

SH

Thanks for the reply, Fred.

I'll add a condition to check if there are no duplicates in the array
to avoid the uniq! gotcha.

That wasn't what I was suggesting. What I meant was that

valid_dates = @shipments.collect {|s| s.ship_date}.uniq
is ok, as is
valid_dates = @shipments.collect {|s| s.ship_date}
valid_dates.uniq!

but
valid_dates = @shipments.collect {|s| s.ship_date}.uniq!

isn't.

Fred

Understood... clearly I need a Ruby primer (any suggestions?)

Thanks again Fred.

SH