# Ruby hashes

**URL:** <https://www.ruby-forum.com/t/ruby-hashes/121009>\
**Category:** Ruby\
**Created:** [November 22, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/ruby-hashes/121009 "2007-11-22T16:54:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [November 22, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/ruby-hashes/121009/1 "2007-11-22T16:54:02Z")

</div>

Is there a way in Ruby to pass a hash value from string (or whatever) to  
an Array dinamically?

Example: An array of objects Teacher, each with his name and belonging  
school. After iterating though its elements, we want an output of the  
type, “school\_name\_A” =\> “teacher\_name\_A”, “school\_name\_B” =\>  
[“teacher\_name\_B”, “teacher\_name\_C”].

…or maybe there is another simpler way of doing this?

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [November 22, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/ruby-hashes/121009/2 "2007-11-22T17:03:21Z")

</div>

Alvaro P. wrote:

> Is there a way in Ruby to pass a hash value from string (or whatever) to  
> an Array dinamically?
> 
> Example: An array of objects Teacher, each with his name and belonging  
> school. After iterating though its elements, we want an output of the  
> type, “school\_name\_A” =\> “teacher\_name\_A”, “school\_name\_B” =\>  
> [“teacher\_name\_B”, “teacher\_name\_C”].  
> It’s easier if you allow:

{“school\_name\_A” =\> [“teacher\_name\_A”],  
“school\_name\_B” =\> [“teacher\_name\_B”, “teacher\_name\_C”]}

That way you can do this:

schools = Hash.new{|h,k| h[k] = []}  
teachers.each {|teacher| schools[teacher.school] \<\< teacher.name}

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [November 22, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/ruby-hashes/121009/3 "2007-11-22T17:05:42Z")

</div>

Alvaro P. wrote:

> Example: An array of objects Teacher, each with his name and belonging  
> school. After iterating though its elements, we want an output of the  
> type, “school\_name\_A” =\> “teacher\_name\_A”, “school\_name\_B” =\>  
> [“teacher\_name\_B”, “teacher\_name\_C”].

hash = Hash.new {|h,k| h[k] = []}  
teachers.each do |teacher|  
hash[teacher.school] \<\< teacher.name  
end

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [November 22, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/ruby-hashes/121009/4 "2007-11-22T17:13:09Z")

</div>

Wow, exactly same solution 😉

but it works! thank you very much to both.
