Sha256: fe53457832021755e5f8079bdc9f3a7c87bd95c69388cbad08f411f1d80baf33

Contents?: true

Size: 528 Bytes

Versions: 2

Compression:

Stored size: 528 Bytes

Contents

# Gist from: https://gist.github.com/745617
module LazyHash
  class << self
    def lazy_add(hash, key, value, pre = nil)
      skeys = key.split(".")
      f = skeys.shift
      if skeys.empty?
        pre.nil? ? hash.send("[]=", f, value) : pre.send("[]=", f, value)
      else
        pre = pre.nil? ? hash.send("[]", f) : pre.send("[]", f)
        lazy_add(hash, skeys.join("."), value, pre)
      end
    end

    def build_hash
      lazy = lambda { |h,k| h[k] = Hash.new(&lazy) }
      Hash.new(&lazy)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
interpret-0.1.2 lib/interpret/lazy_hash.rb
interpret-0.1.1 lib/interpret/lazy_hash.rb