Skip to content

Instantly share code, notes, and snippets.

@JamesChevalier
Created February 4, 2016 18:08
Show Gist options
  • Save JamesChevalier/53d8fe6818e45ff2ce98 to your computer and use it in GitHub Desktop.
Save JamesChevalier/53d8fe6818e45ff2ce98 to your computer and use it in GitHub Desktop.
Create an infinitely nestable hash in Ruby
# 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