Module: Doing::ArrayNestedHash

Defined in:
lib/doing/array/nested_hash.rb

Overview

Array helpers

Instance Method Summary collapse

Instance Method Details

#nested_hash(value = nil) ⇒ Object

Convert array to nested hash, setting last key to value

Parameters:

  • value (defaults to: nil)

    The value to set



11
12
13
14
15
# File 'lib/doing/array/nested_hash.rb', line 11

def nested_hash(value = nil)
  hsh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
  hsh.dig(*self[0..-2])[fetch(-1)] = value
  hsh
end