# Creating an array of hashes and having issues

**URL:** <https://www.ruby-forum.com/t/creating-an-array-of-hashes-and-having-issues/193276>\
**Category:** Ruby\
**Created:** [August 14, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hashes-and-having-issues/193276 "2010-08-14T16:17:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 14, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hashes-and-having-issues/193276/1 "2010-08-14T16:17:46Z")

</div>

I have an array that contains 36 objects. The composition of those  
objects includes a date, a team, and a time. It is being parsed from  
another site.

[0] = Sept 2  
[1] = vsSouthern Miss  
[2] = 7:30 PM ET  
[3] = Sept 9  
[4] = @Florida  
[5] = 7:30 PM ET  
etc.

Every 3 objects should be combined into a specific hash row.

[0][:date] = Sept 2  
[0][:team] = vsSouthern Miss  
[0][:time] = 7:30 PM ET  
[1][:date] = Sept 9  
[1][:team] = @Florida  
[1][:time] = 7:30 PM ET  
etc.

I’m having trouble accomplishing this. Each schedule might have more  
than 36 objects in the array, but the divisor will always be 3 in terms  
of what type of information is presented. So, a team might have an  
array of 39 objects or 42 objects but in the end, the objects are a  
date, team, and a time.

I’ve tried numerous things, even adding a new class method:

class Array  
def to\_spec\_hash(other)  
Hash[\*(0…self.size()).inject([]) { |arr, ix| arr.push(self[ix],  
other[ix]) } ]  
end  
end

and then trying to use:

%W{ date team time }.to\_spec\_hash( %W{ value1 value2 value3 etc. } )

but I’d have to iterate over date team and time according to how many  
rows of data divided by 3 would be and the same for the values. Again,  
I know it’s sloppy.

I’m sure there’s a much faster and a cleaner way of accomplishing this  
but I’m lacking sleep and therefore not thinking as clearly.

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 14, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hashes-and-having-issues/193276/2 "2010-08-14T18:25:43Z")

</div>

On Sat, Aug 14, 2010 at 10:17 AM, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> etc.
> 
> Hash[\*(0…self.size()).inject([]) { |arr, ix| arr.push(self[ix],  
> I know it’s sloppy.
> 
> I’m sure there’s a much faster and a cleaner way of accomplishing this  
> but I’m lacking sleep and therefore not thinking as clearly.
> 
> Any help would be appreciated.

Is this what you want?

input = [  
“Sept 2”,  
“vsSouthern Miss”,  
“7:30 PM ET” ,  
“Sept 9” ,  
“@Florida” ,  
“7:30 PM ET”  
]

input.extend Enumerable

p input.enum\_for(:each\_slice, 3).map { |date, team, time|  
{:date =\> date, :team =\> team, :time =\> time}

}

[{:date=\>“Sept 2”, :team=\>“vsSouthern Miss”, :time=\>“7:30 PM ET”},  
{:date=\>“Sept 9”, :team=\>“@Florida”, :time=\>“7:30 PM ET”}]

This is written for 1.8.6, for 1.8.7 or 1.9.x Enumerable is built in  
in 1.9 and you can get the enumerator by calling each\_slice without a  
block.

p (input.each\_slice(3)).map { |date, team, time|  
{:date =\> date, :team =\> team, :time =\> time}

}

## In any event if it were me I’d make a Game class, or at least use an OpenStruct instead of a Hash

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [August 14, 2010, 8:05pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-hashes-and-having-issues/193276/3 "2010-08-14T20:05:59Z")

</div>

Thanks Rick - this example did work but I believe your second idea was a  
better one. I’ll look into that instead.
