Module: HashHelper::ToNestedH

Included in:
Array
Defined in:
lib/hash_helper/to_nested_h.rb

Instance Method Summary collapse

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.

Examples:

Single level array

[[:a, :b]].to_nested_h
# => {:a => nil, :b => nil}

Two levels of nesting

[[:a, :b], [:x, :y]].to_nested_h(value: 0)
# => {:a => {:x => 0, :y => 0}, :b => {:x => 0, :y => 0}}

Multiple levels of nesting

[[:a, :b], [:x, :y], [:m, :n]].to_nested_h(value: "leaf")
# => {
#   :a => { :x => { :m => "leaf", :n => "leaf" }, :y => { :m => "leaf", :n => "leaf" } },
#   :b => { :x => { :m => "leaf", :n => "leaf" }, :y => { :m => "leaf", :n => "leaf" } }
# }

Parameters:

  • value (Object) (defaults to: nil)

    The default value to assign to the leaf nodes (default: nil).

Returns:

  • (Hash)

    A nested hash structure with the specified default value at the leaves.



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