# Difficulty nesting array in hash

**URL:** <https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583>\
**Category:** Rails\
**Created:** [July 31, 2008, 7:39am UTC](https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583 "2008-07-31T07:39:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 31, 2008, 7:39am UTC](https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583/1 "2008-07-31T07:39:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 31, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583/2 "2008-07-31T17:05:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 31, 2008, 11:56am UTC](https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583/3 "2008-07-31T11:56:39Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 31, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583/4 "2008-07-31T21:53:10Z")

</div>

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

Thanks again Fred.

SH

On Jul 31, 8:34 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 31, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/difficulty-nesting-array-in-hash/144583/5 "2008-07-31T17:34:48Z")

</div>

On 31 Jul 2008, at 16:04, shenry wrote:

> 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
