# Hash.collect

**URL:** <https://www.ruby-forum.com/t/hash-collect/84146>\
**Category:** Ruby\
**Created:** [February 1, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/hash-collect/84146 "2007-02-01T21:49:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [February 1, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/hash-collect/84146/1 "2007-02-01T21:49:36Z")

</div>

Hi,

I am pulling my hair out trying to figure out the following problem (my  
fault not rubys):

I have an array of hashes as follows:

a = Array.new  
a \<\< {:name =\> ‘tony’, :age =\> 23}  
a \<\< {:name =\> ‘mary’, :age =\> 57}  
a \<\< {:name =\> ‘dom’, :age =\> 17}

I am trying to create an array with one attribute from each hash:

[‘tony’, ‘mary’, ‘dom’]

Any clues on how I should do this would be much appreciated.

Thanks,  
GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [February 1, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/hash-collect/84146/2 "2007-02-01T21:56:07Z")

</div>

On Feb 1, 2007, at 2:49 PM, Giant C. wrote:

> I have an array of hashes as follows:
> 
> a = Array.new  
> a \<\< {:name =\> ‘tony’, :age =\> 23}  
> a \<\< {:name =\> ‘mary’, :age =\> 57}  
> a \<\< {:name =\> ‘dom’, :age =\> 17}
> 
> I am trying to create an array with one attribute from each hash:
> 
> [‘tony’, ‘mary’, ‘dom’]

> > a = Array.new  
> > =\> []
> 
> > a \<\< {:name =\> ‘tony’, :age =\> 23}  
> > =\> [{:name=\>“tony”, :age=\>23}]
> 
> > a \<\< {:name =\> ‘mary’, :age =\> 57}  
> > =\> [{:name=\>“tony”, :age=\>23}, {:name=\>“mary”, :age=\>57}]
> 
> > a \<\< {:name =\> ‘dom’, :age =\> 17}  
> > =\> [{:name=\>“tony”, :age=\>23}, {:name=\>“mary”, :age=\>57},  
> > {:name=\>“dom”, :age=\>17}]
> 
> > a.map { |e| e[:name] }  
> > =\> [“tony”, “mary”, “dom”]

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [February 1, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/hash-collect/84146/3 "2007-02-01T21:58:23Z")

</div>

Wonderful, thanks very much.

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [February 1, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/hash-collect/84146/4 "2007-02-01T22:02:08Z")

</div>

On Fri, Feb 02, 2007 at 05:49:38AM +0900, Giant C. wrote:

> a = Array.new  
> a \<\< {:name =\> ‘tony’, :age =\> 23}  
> a \<\< {:name =\> ‘mary’, :age =\> 57}  
> a \<\< {:name =\> ‘dom’, :age =\> 17}
> 
> I am trying to create an array with one attribute from each hash:
> 
> [‘tony’, ‘mary’, ‘dom’]
> 
> Any clues on how I should do this would be much appreciated.

b = a.collect { |e| e[:name] }

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [February 1, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/hash-collect/84146/5 "2007-02-01T22:06:13Z")

</div>

On Feb 1, 2007, at 3:49 PM, Giant C. wrote:

> I am trying to create an array with one attribute from each hash:
> 
> [‘tony’, ‘mary’, ‘dom’]
> 
> Any clues on how I should do this would be much appreciated.

a.map { |h| h[:name] }

Gary W.

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [February 1, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/hash-collect/84146/6 "2007-02-01T22:29:16Z")

</div>

On Fri, Feb 02, 2007 at 05:49:38AM +0900, Giant C. wrote:

> a \<\< {:name =\> ‘dom’, :age =\> 17}
> 
> I am trying to create an array with one attribute from each hash:
> 
> [‘tony’, ‘mary’, ‘dom’]
> 
> Any clues on how I should do this would be much appreciated.

a.collect {|e| e[:name]}
