Method: Array#nested_hash

Defined in:
lib/doing/array.rb

#nested_hash(value) ⇒ Object

Convert array to nested hash, setting last key to value

Parameters:

  • value

    The value to set

Raises:

  • (StandardError)


58
59
60
61
62
63
64
# File 'lib/doing/array.rb', line 58

def nested_hash(value)
  raise StandardError, 'Value can not be nil' if value.nil?

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