# Filling a hash from an enumerable

**URL:** <https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490>\
**Category:** Ruby\
**Created:** [October 9, 2008, 3:03am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490 "2008-10-09T03:03:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 3:03am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/1 "2008-10-09T03:03:20Z")

</div>

Hi folks,

Given:

Person = Struct.new(:name,:age,:city)  
people\_array = [  
Person.new(‘bob’,12,‘SFO’),  
Person.new(‘dave’,14,‘NYC’),  
Person.new(‘jane’,6,‘LDN’) ]

people\_map = {}  
people\_array.each{ |p| people\_map[p.name] = p }

Is there a cleaner way to build people\_map? I’m thinking there might be  
something like:

people\_map = people\_array.to\_map{ |x| x.name }

in the standard library somewhere that I don’t know of.

Thanks for any pointers,  
Pete

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 3:42am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/2 "2008-10-09T03:42:01Z")

</div>

From: Pete H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# people\_map = people\_array.to\_map{ |x| x.name }

you should try it,

> > people\_array.map{ |p| p.name }  
> > =\> [“bob”, “dave”, “jane”]

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 4:56am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/3 "2008-10-09T04:56:55Z")

</div>

On Oct 8, 9:00 pm, Pete H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> people\_map = {}  
> people\_array.each{ |p| people\_map[p.name] = p }
> 
> Is there a cleaner way to build people\_map? I’m thinking there might be  
> something like:
> 
> people\_map = people\_array.to\_map{ |x| x.name }
> 
> in the standard library somewhere that I don’t know of.

Facets has #graph / #mash.

require ‘facets’

people\_array.graph{ |p| [p.name, p] }

# or

people\_array.mash{ |p| [p.name, p] }

T.

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 9:13am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/4 "2008-10-09T09:13:17Z")

</div>

Peña, Botp wrote:

> > 
> 
> =\> {“bob”=\>[#\<struct Person name=“bob”, age=12, city=“SFO”\>], “dave”=\>[#\<struct Person name=“dave”, age=14, city=“NYC”\>], “jane”=\>[#\<struct Person name=“jane”, age=6, city=“LDN”\>]}

Thanks, but that’s not quite what I wanted. It creates a hash whose  
values are lonely arrays, rather than a map whose values are the structs  
themselves.

I guess I’ll just have to monkey-patch Enumerable with a to\_map method.  
Should probably figure out a better name first tho.

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 5:09am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/5 "2008-10-09T05:09:13Z")

</div>

From: Peña, Botp [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# \>\> people\_array.map{ |p| p.name }

# =\> [“bob”, “dave”, “jane”]

pls ignore above, i’m still searching my lost brain 🙂

try #group\_by,

> people\_array.group\_by{ |p| p.name }  
> =\> {“bob”=\>[#\<struct Person name=“bob”, age=12, city=“SFO”\>],  
> “dave”=\>[#\<struct Person name=“dave”, age=14, city=“NYC”\>],  
> “jane”=\>[#\<struct Person name=“jane”, age=6, city=“LDN”\>]}

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 9:25am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/6 "2008-10-09T09:25:37Z")

</div>

On Oct 8, 8:00 pm, Pete H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> people\_map = {}  
> Pete  
> Hash[\* people\_array.map{|x| [ x.name, x] }.flatten ]  
> ==\>{“dave” =\> #\<struct Person name=“dave”, age=14, city=“NYC”\>,  
> “jane” =\> #\<struct Person name=“jane”, age=6, city=“LDN”\>,  
> “bob” =\> #\<struct Person name=“bob”, age=12, city=“SFO”\>}

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 9:33am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/7 "2008-10-09T09:33:49Z")

</div>

2008/10/9 William J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > something like:  
> > Hash[\* people\_array.map{|x| [ x.name, x] }.flatten ]  
> > ==\>{“dave” =\> #\<struct Person name=“dave”, age=14, city=“NYC”\>,  
> > “jane” =\> #\<struct Person name=“jane”, age=6, city=“LDN”\>,  
> > “bob” =\> #\<struct Person name=“bob”, age=12, city=“SFO”\>}

And since we did not have an inject version so far:

irb(main):001:0\> require ‘pp’  
=\> true  
irb(main):002:0\> Person = Struct.new(:name,:age,:city)  
=\> Person  
irb(main):003:0\> people\_array = [  
irb(main):004:1\* Person.new(‘bob’,12,‘SFO’),  
irb(main):005:1\* Person.new(‘dave’,14,‘NYC’),  
irb(main):006:1\* Person.new(‘jane’,6,‘LDN’),  
irb(main):007:1\* ]  
=\> [#\<struct Person name=“bob”, age=12, city=“SFO”\>, #\<struct Person  
name=“dave”, age=14, city=“NYC”\>, #\<struct Person name=“jane”, age=6,  
city=“LDN”\>]  
irb(main):008:0\> pp( people\_map = people\_array.inject({}) do |ha,pe|  
irb(main):009:2\* ha[pe.name] = pe  
irb(main):010:2\> ha  
irb(main):011:2\> end )  
{“dave”=\>#\<struct Person name=“dave”, age=14, city=“NYC”\>,  
“jane”=\>#\<struct Person name=“jane”, age=6, city=“LDN”\>,  
“bob”=\>#\<struct Person name=“bob”, age=12, city=“SFO”\>}  
=\> nil

Just for completeness reasons of course. 😉

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 11:48am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/8 "2008-10-09T11:48:59Z")

</div>

On Oct 9, 3:10 am, Pete H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess I’ll just have to monkey-patch Enumerable with a to\_map method.  
> Should probably figure out a better name first tho.

You mean like “map hash”, aka “mash”.

T.

---

<div class="post-metadata">

**Author:** ![moredip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@moredip](https://www.ruby-forum.com/u/moredip)\
**Post date:** [October 9, 2008, 11:38am UTC](https://www.ruby-forum.com/t/filling-a-hash-from-an-enumerable/150490/9 "2008-10-09T11:38:22Z")

</div>

From: Pete H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Peña, Botp wrote:

# \>\> people\_array.group\_by{ |p| p.name }

# \> =\> {“bob”=\>[#\<struct Person name=“bob”, age=12,

# city=“SFO”\>], “dave”=\>[#\<struct Person name=“dave”, age=14,

# city=“NYC”\>], “jane”=\>[#\<struct Person name=“jane”, age=6,

# city=“LDN”\>]}

# \>

# Thanks, but that’s not quite what I wanted. It creates a hash whose

# values are lonely arrays,…

lonely indeed, but consider the case when you have dup names

kind regards -botp
