# Help using ruby enumerator idioms

**URL:** <https://www.ruby-forum.com/t/help-using-ruby-enumerator-idioms/156700>\
**Category:** Ruby\
**Created:** [January 1, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/help-using-ruby-enumerator-idioms/156700 "2009-01-01T13:39:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mischa](https://www.ruby-forum.com/u/mischa)\
**Post date:** [January 1, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/help-using-ruby-enumerator-idioms/156700/1 "2009-01-01T13:39:47Z")

</div>

Hi,

In Ruby, when I see something like:

h = []

x.each do

…

end

h

Usually means that I can refactor it to use map, inject, select, or  
reject.

However, I am unsure how to do this for the code below:

```
     h = Hash.new {|hash, key| hash[key] = {}}

```

some\_hash\_of\_hashes.each do |hash\_key, sub\_hash|  
sub\_hash.each {|key, value| h[key][hash\_key] = value }  
end

h

Let me know if anyone has any ideas.

M

---

<div class="post-metadata">

**Author:** ![mischa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mischa](https://www.ruby-forum.com/u/mischa)\
**Post date:** [January 2, 2009, 10:47am UTC](https://www.ruby-forum.com/t/help-using-ruby-enumerator-idioms/156700/2 "2009-01-02T10:47:03Z")

</div>

> But here inner\_acc is always identical to acc, making the inject just  
> for show. You’re back to using #each.
> 
> I guess the moral of the story is that functional style in ruby  
> realistically applies only to flat arrays and hashes. Once we reach  
> the hash-of-hashes realm, imperative constructs are more suitable.
> 
> Lazy evaluation is needed to be efficient, recursive, and purely  
> functional all at the same time. (See Haskell.)

Interesting, thanks a ton for the reply.

I think you may be right that for hashes of hashes imperative style Ruby  
starts to make more sense.

This has been hitting me a lot lately, as one of the projects I’m  
working on has a lot of hashes of hashes (data crunching). I used the  
inject style a few times, but as you note, it becomes mostly just for  
show.

M

---

<div class="post-metadata">

**Author:** ![mischa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mischa](https://www.ruby-forum.com/u/mischa)\
**Post date:** [January 2, 2009, 9:34am UTC](https://www.ruby-forum.com/t/help-using-ruby-enumerator-idioms/156700/3 "2009-01-02T09:34:57Z")

</div>

Mischa F. wrote:

> Hi,
> 
> In Ruby, when I see something like:
> 
> h = []
> 
> x.each do
> 
> …
> 
> end
> 
> h
> 
> Usually means that I can refactor it to use map, inject, select, or  
> reject.

Yes, I do the same. In a broader sense, functional style is often  
shorter and less error-prone. Map, inject, select are just examples  
of the functional idiom smuggled into ruby.

In this case, a hash-of-hash merge function is needed to reorder keys  
in a clean way.

# 

# hoh\_merge(

# { :x =\> { :y =\> :foo } },

# { :x =\> { :z =\> :bar } }

# )

# #=\> {:x=\>{:y=\>:foo, :z=\>:bar}}

# 

def hoh\_merge(a, b)  
b.inject(a) { |acc, (key, inner\_hash)|  
acc.merge(  
key =\> (  
if existing = a[key]  
existing.merge(inner\_hash)  
else  
inner\_hash  
end  
)  
)  
}  
end

data = {  
:flintstone =\> {  
:husband =\> :fred,  
:wife =\> :wilma,  
},  
:rubble =\> {  
:husband =\> :barney,  
:wife =\> :betty,  
},  
}

# original imperative version

h = Hash.new { |hash, key| hash[key] = Hash.new }  
data.each { |hash\_key, inner\_hash|  
inner\_hash.each { |key, value|  
h[key][hash\_key] = value  
}  
}

# pure functional version

h2 = data.inject(Hash.new) { |acc, (hash\_key, inner\_hash)|  
inner\_hash.keys.inject(acc) { |inner\_acc, key|  
hoh\_merge(inner\_acc, key =\> { hash\_key =\> inner\_hash[key] })  
}  
}

require ‘pp’

pp h  
pp h2

#=\>

# {:wife=\>{:rubble=\>:betty, :flintstone=\>:wilma},

# :husband=\>{:rubble=\>:barney, :flintstone=\>:fred}}

# {:wife=\>{:rubble=\>:betty, :flintstone=\>:wilma},

# :husband=\>{:rubble=\>:barney, :flintstone=\>:fred}}

I wrote a purely-functional hoh\_merge just for fun. An imperative  
method would be more efficient (modifying its first argument),

h2 = data.inject(Hash.new) { |acc, (hash\_key, inner\_hash)|  
inner\_hash.keys.inject(acc) { |inner\_acc, key|  
hoh\_merge!(inner\_acc, key =\> { hash\_key =\> inner\_hash[key] })  
}  
}

But here inner\_acc is always identical to acc, making the inject just  
for show. You’re back to using #each.

I guess the moral of the story is that functional style in ruby  
realistically applies only to flat arrays and hashes. Once we reach  
the hash-of-hashes realm, imperative constructs are more suitable.

Lazy evaluation is needed to be efficient, recursive, and purely  
functional all at the same time. (See Haskell.)
