opensoul.org

The Ruby Infinite Hash

December 13, 2010 code 1 min read

@tenderlove shows us ruby magic to create an infinite hash:

>> hash = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
 => {}
>> hash['a'] = 1
 => 1
>> hash['a']
 => 1
>> hash['b']
 => {}
>> hash['b']['c']['d']
 => {}
This content is open source. Suggest Improvements.

@bkeepers

avatar of Brandon Keepers I am Brandon Keepers, and I work at GitHub on making Open Source more approachable, effective, and ubiquitous. I tend to think like an engineer, work like an artist, dream like an astronaut, love like a human, and sleep like a baby.