Sha256: e10aea2e0a342f57c3af0cca291cc9e768ef8f3db8295f95d42f544d0ea00a3d

Contents?: true

Size: 358 Bytes

Versions: 14

Compression:

Stored size: 358 Bytes

Contents

module Doing
  ##
  ## Array helpers
  ##
  class ::Array
    ##
    ## Convert array to nested hash, setting last key to value
    ##
    ## @param      value  The value to set
    ##
    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
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
doing-2.1.40 lib/doing/array/nested_hash.rb
doing-2.1.39 lib/doing/array/nested_hash.rb
doing-2.1.38 lib/doing/array/nested_hash.rb
doing-2.1.37 lib/doing/array/nested_hash.rb
doing-2.1.36 lib/doing/array/nested_hash.rb
doing-2.1.35 lib/doing/array/nested_hash.rb
doing-2.1.34 lib/doing/array/nested_hash.rb
doing-2.1.33 lib/doing/array/nested_hash.rb
doing-2.1.32 lib/doing/array/nested_hash.rb
doing-2.1.31pre lib/doing/array/nested_hash.rb
doing-2.1.30 lib/doing/array/nested_hash.rb
doing-2.1.29 lib/doing/array/nested_hash.rb
doing-2.1.28 lib/doing/array/nested_hash.rb
doing-2.1.27 lib/doing/array/nested_hash.rb