# Flatten out Hash

**URL:** <https://www.ruby-forum.com/t/flatten-out-hash/184843>\
**Category:** Ruby\
**Created:** [March 9, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843 "2010-03-09T13:31:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [March 9, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/1 "2010-03-09T13:31:48Z")

</div>

Hi,

I would like to take a nested hash that looks like this:

{“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}

and turn it into an array of 2 element arrays like this:

[["a: b2: c2: ", 2], ["a: b: c: ", 1]]

Is there a simple way to do this? I wrote a method that iterates  
through the nested hashes recursively, but it’s a bit cumbersome.

---

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [March 9, 2010, 1:52pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/2 "2010-03-09T13:52:22Z")

</div>

On Tue, Mar 9, 2010 at 8:31 PM, Glenn R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I would like to take a nested hash that looks like this:
> 
> {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}
> 
> and turn it into an array of 2 element arrays like this:
> 
> [["a: b2: c2: ", 2], ["a: b: c: ", 1]]

My method:

hash = {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}  
array =

hash.keys.each do |k1|  
hash[k1].keys.each do |k2|  
hash[k1][k2].keys.each do |k3|  
array \<\< [“#{k1}: #{k2}: #{k3}:”,hash[k1][k2][k3]]  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [March 9, 2010, 2:05pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/3 "2010-03-09T14:05:52Z")

</div>

Jean G. wrote:

> On Tue, Mar 9, 2010 at 8:31 PM, Glenn R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > I would like to take a nested hash that looks like this:
> > 
> > {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}
> > 
> > and turn it into an array of 2 element arrays like this:
> > 
> > [["a: b2: c2: ", 2], ["a: b: c: ", 1]]
> 
> My method:
> 
> hash = {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}  
> array =
> 
> hash.keys.each do |k1|  
> hash[k1].keys.each do |k2|  
> hash[k1][k2].keys.each do |k3|  
> array \<\< [“#{k1}: #{k2}: #{k3}:”,hash[k1][k2][k3]]  
> end  
> end  
> end

Thanks, Jean.

It’s a good solution. But it’s dependent on there being 3 levels of  
hashes. What if you didn’t know how many levels of nesting there was  
before executing the code? Suppose the hash looked like this instead:

{“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\> {“d2” =\> 2}}}}

---

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [March 9, 2010, 2:15pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/4 "2010-03-09T14:15:55Z")

</div>

Glenn R. wrote:

> Hi,
> 
> I would like to take a nested hash that looks like this:
> 
> {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}
> 
> and turn it into an array of 2 element arrays like this:
> 
> [["a: b2: c2: ", 2], ["a: b: c: ", 1]]
> 
> Is there a simple way to do this? I wrote a method that iterates  
> through the nested hashes recursively, but it’s a bit cumbersome.

I think that’s the right way, if the nesting depth is variable. One  
example:

class Hash  
def flat\_each(prefix=“”, &blk)  
each do |k,v|  
if v.is\_a?(Hash)  
v.flat\_each(prefix+k+": ", &blk)  
else  
yield prefix+k, v  
end  
end  
end  
end

require ‘enumerator’  
h = {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}  
p h.to\_enum(:flat\_each).collect { |k,v| [k,v] }

---

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [March 9, 2010, 2:25pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/5 "2010-03-09T14:25:18Z")

</div>

Perhaps a better solution is to yield an array of keys (being the ‘path’  
to reach the end node), because then the caller can choose how to  
combine them.

class Hash  
def flat\_each(prefix=[], &blk)  
each do |k,v|  
if v.is\_a?(Hash)  
v.flat\_each(prefix+[k], &blk)  
else  
yield prefix+[k], v  
end  
end  
end  
end

require ‘enumerator’  
h = {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\>2}}}  
h = {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\> {“d2” =\> 2}}}}  
p h.to\_enum(:flat\_each).collect { |k,v| [k.join(": “),v] }  
h.flat\_each do |k,v|  
puts “#{k.join(”/”)} =\> #{v}"  
end

---

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [June 24, 2011, 12:27am UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/6 "2011-06-24T00:27:51Z")

</div>

I coded up a way to do this in a method without needing to monkey-patch  
Hash: [Yaeger Design | Professional Website Design Dalton, Georgia (GA) | Dalton Web Designer](http://forr.st/~zaG)

# Takes in a hash in input and merges it with an output hash to produce

a hash that is only one dimensional

# i.e. there are no nested elements in the hash

def make\_hash\_one\_dimensional(input = {}, output = {}, options = {})  
input.each do |key, value|  
key = options[:prefix].nil? ? “#{key}” :  
“#{options[:prefix]}#{options[:delimiter]||”_“}#{key}”  
if value.is\_a? Hash  
make\_hash\_one\_dimensional(value, output, :prefix =\> key,  
:delimiter =\> "_")  
else  
output[key] = value  
end  
end  
output  
end

hash = {:a =\> {:b =\> {:c =\> {:d =\> {:e =\> “hi”}}}}, :f =\> “there”}  
make\_hash\_one\_dimensional(hash)  
#=\> {“f”=\>“there”, “a\_b\_c\_d\_e”=\>“hi”}

From there it would be easy to turn this into an array. Enjoy

–  
@schneems

---

<div class="post-metadata">

**Author:** ![glenn\_nz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3e96dc/32.png) [@glenn\_nz](https://www.ruby-forum.com/u/glenn_nz)\
**Post date:** [March 9, 2010, 2:26pm UTC](https://www.ruby-forum.com/t/flatten-out-hash/184843/7 "2010-03-09T14:26:21Z")

</div>

2010/3/9 Glenn R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > [["a: b2: c2: ", 2], [“a: b: c: “, 1]]  
> > > array \<\< [”#{k1}: #{k2}: #{k3}:”,hash[k1][k2][k3]]  
> > > {“a”=\>{“b”=\>{“c”=\>1}, “b2”=\>{“c2”=\> {“d2” =\> 2}}}}  
> > > Basically you want to do a DFS and store the path to the root for  
> > > every leaf. Something like [DFS in nested Hashes · GitHub](http://gist.github.com/326558)

Kind regards

robert
