# Is there an idiom for mass-populating Hash keys?

**URL:** <https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184>\
**Category:** Ruby\
**Created:** [March 29, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184 "2009-03-29T18:07:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [March 29, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184/1 "2009-03-29T18:07:04Z")

</div>

Hello, good folk of ruby-talk.

I was wondering whether thereâ€™s a Ruby  
idiom to mass-populate keys of a Hash.

Iâ€™m coding a graph class supporting labeled vertices, and I want to  
represent this relationship as a @vertices Hash, with keys being the  
actual vertices and values being their labels â€“ initially all nil:

class Graph  
def initialize enum  
@vertices = {}  
enum.each { |vertex| @vertices[vertex] = nil }  
end  
end

Is there a more elegant way to take an enum  
and turn it into nil-referencing Hash keys?

â€” Shot

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [March 29, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184/2 "2009-03-29T18:17:02Z")

</div>

On Mar 29, 2009, at 10:05 AM, Shot (Piotr S.) wrote:

> def initialize enum  
> @vertices = {}  
> enum.each { |vertex| @vertices[vertex] = nil }  
> end  
> end
> 
> Is there a more elegant way to take an enum  
> and turn it into nil-referencing Hash keys?

dunno if it’s really better but this is one approach:

cfp:~ \> cat a.rb

hash = {}

enum = (‘a’…‘z’)

hash.update Hash[\*enum.zip([])]

p hash  
cfp:~ \> ruby a.rb  
{[“e”, nil]=\>[“f”, nil], [“o”, nil]=\>[“p”, nil], [“y”, nil]=\>[“z”,  
nil], [“i”, nil]=\>[“j”, nil], [“s”, nil]=\>[“t”, nil], [“c”,  
nil]=\>[“d”, nil], [“m”, nil]=\>[“n”, nil], [“w”, nil]=\>[“x”, nil],  
[“g”, nil]=\>[“h”, nil], [“q”, nil]=\>[“r”, nil], [“a”, nil]=\>[“b”,  
nil], [“k”, nil]=\>[“l”, nil], [“u”, nil]=\>[“v”, nil]}

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [March 29, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184/3 "2009-03-29T18:57:34Z")

</div>

On Sun, Mar 29, 2009 at 12:05 PM, Shot (Piotr S.)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> def initialize enum  
> @vertices = {}  
> enum.each { |vertex| @vertices[vertex] = nil }  
> end  
> end
> 
> Is there a more elegant way to take an enum  
> and turn it into nil-referencing Hash keys?

Why bother? The only reason I can thing of is that you want to use  
@vertices.keys to get the collection of vertices, but why be indirect.

class Graph  
def initialize(enum)  
@vertices = enum  
@labels = {}  
end  
end

@labels[anything] will return nil if anything isn’t a key.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [March 29, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184/4 "2009-03-29T22:32:59Z")

</div>

Rick DeNatale:

> On Sun, Mar 29, 2009 at 12:05 PM, Shot (Piotr S.) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > class Graph  
> > def initialize enum  
> > @vertices = {}  
> > enum.each { |vertex| @vertices[vertex] = nil }  
> > end  
> > end

> Why bother? The only reason I can thing of is that you want to use  
> @vertices.keys to get the collection of vertices, but why be indirect.

> class Graph  
> def initialize(enum)  
> @vertices = enum  
> @labels = {}  
> end  
> end

> @labels[anything] will return nil if anything isn’t a key.

Right, I was thinking about this approach on and off all day today,  
actually. 🙂 The catch is Iâ€™ll be doing various things to this graph,  
like merging vertices together, grouping them by their labels (graph  
colouring), and doing similar operations on (labeled) edges.

While I do like the cleanliness of the approach with @vertices being  
just a Set, @edges being just a Set of two-element Sets, and there being  
two additional Hashes for vertex and edge labels, Iâ€™m not sure I want  
the overhead of making sure there arenâ€™t any leftover keys in the label  
Hashes after the graph drops some of its edges/vertices â€“ in other  
words, anytime I delete a vertex or an edge Iâ€™d have to delete it from  
two places (either that, or anytime I query the labels Iâ€™d have to make  
sure Iâ€™m filtering out no-longer-existing vertices/edges â€“ which is even  
uglier).

Note: I do know about GRATR, but it seems unmaintained; I found  
(granted, minor) bugs in it by simply requiring it with warnings  
enabled, and it requires (again, minor) patching to get it working  
with Ruby 1.9, but my patches to fix both are ignored (and there havenâ€™t  
been a relase for a year and a half). I would also use just a tiny  
fraction of GRATRâ€™s functionality, so itâ€™s simpler for me to have my  
own Graph class (and be able to easily RubyInline parts of it if/when  
I need the performance).

â€” Shot

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [March 31, 2009, 1:15am UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184/5 "2009-03-31T01:15:59Z")

</div>

Shot (Piotr S.) wrote:

> The catch is Iâ€™ll be doing various things to this graph,  
> like merging vertices together, grouping them by their labels (graph  
> colouring), and doing similar operations on (labeled) edges.

Try providing a default\_proc to Hash.new:

Hash.new {|h,k| h[k] = nil }

Now previously unregistered vertices will be registered upon hash access  
(Hash#[]). To illustrate this technique, consider this “dynamic  
programming” based Fibonacci sequence implementation:

fib = Hash.new {|h,k| k \< 2 ? 1 : h[k-1] + h[k-2] }

p 0 =\> fib[0]  
p 1 =\> fib[1]  
p 5 =\> fib[5]  
p :cache =\> fib

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [March 31, 2009, 1:18am UTC](https://www.ruby-forum.com/t/is-there-an-idiom-for-mass-populating-hash-keys/164184/6 "2009-03-31T01:18:56Z")

</div>

Suraj K. wrote:

> consider this “dynamic programming” based Fibonacci sequence implementation:
> 
> fib = Hash.new {|h,k| k \< 2 ? 1 : h[k-1] + h[k-2] }

Whoops, I forgot to store the computed value in the hash!

fib = Hash.new {|h,k| h[k] = k \< 2 ? 1 : h[k-1] + h[k-2] }

Now it should work. Sigh 🙂
