Module: HashHelper::ToNestedH
- Included in:
- Array
- Defined in:
- lib/hash_helper/to_nested_h.rb
Instance Method Summary collapse
-
#to_nested_h(value: nil) ⇒ Hash
Converts a nested array into a nested hash with a default value at leaf nodes.
Instance Method Details
#to_nested_h(value: nil) ⇒ Hash
Converts a nested array into a nested hash with a default value at leaf nodes. Each array in the input represents a level of keys in the resulting hash.
23 24 25 26 27 28 29 30 |
# File 'lib/hash_helper/to_nested_h.rb', line 23 def to_nested_h(value: nil) return {} if empty? return first.product([value]).to_h if size == 1 first.map do |key| [key, self[1..].to_nested_h(value: value)] end.to_h end |