Created
February 4, 2016 18:08
-
-
Save JamesChevalier/53d8fe6818e45ff2ce98 to your computer and use it in GitHub Desktop.
Create an infinitely nestable hash in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create the empty hash | |
| nestable_hash = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) } | |
| # Populate the hash with multiple levels of data | |
| nestable_hash[:level_one][:level_two][:level_three][:level_four] = 'mic check' | |
| nestable_hash[:level_one][:level_two_a][:level_three_a] = [1, 2, 1, 2] | |
| # The hash structure is now: | |
| # { | |
| # level_one: { | |
| # level_two: { | |
| # level_three: { | |
| # level_four: 'mic check' | |
| # } | |
| # }, | |
| # level_two_a: { | |
| # level_three_a: [ | |
| # [0] 1, | |
| # [1] 2, | |
| # [2] 1, | |
| # [3] 2 | |
| # ] | |
| # } | |
| # } | |
| # } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment