Sha256: fe3001fc85a83cedf83d8073bd9a5445ca1c185cfadce8fda90578523d3cfb69

Contents?: true

Size: 730 Bytes

Versions: 2

Compression:

Stored size: 730 Bytes

Contents

class Hash
  def get_by_keys *keys
    case keys.size
    when 0
      self
    when 1
      self[keys.first]
    else
      key = keys.shift
      if self.key? key
        self[key].get_by_keys(*keys)
      else
        nil
      end
    end
  end

  def set_by_keys keys, value
    key = keys.shift

    if keys.empty?
      self[key] = value
    else
      self[key] ||= {}
      self[key].set_by_keys keys, value
    end
  end

  def keys? *keys
    h = self
    keys.each_with_index do |key, i|
      if h.key? key
        unless i == (keys.size-1)
          return false unless h[key].is_a? Hash
          h = h[key]
        else
          return true
        end
      else
        return false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
midiops-0.3.1 lib/core_ext/hash.rb
midiops-0.3.0 lib/core_ext/hash.rb