Sha256: 93abbc6115dcb5d2d6b08c46f9b2ecb00beaed99919f0233b04fb31d1156775a

Contents?: true

Size: 527 Bytes

Versions: 2

Compression:

Stored size: 527 Bytes

Contents

# frozen_string_literal: true

class Hash

  ##
  # Check if current hash is sub hash of given hash
  # {a: 1, b: 2}.sub_hash_of?({a: 1, b: 2, c: 3, d: 4}) => true
  # {a: 1, b: 2}.sub_hash_of?({a: 1, b: 5, c: 3, d: 4}) => false
  def sub_hash_of?(hash)
    hash.merge(self) == hash
  end

  ##
  # Check if given hash is sub hash of current hash
  # {a: 1, b: 2, c: 3, d: 4}.sub_hash?({a: 1, b: 2}) => true
  # {a: 1, b: 2, c: 3, d: 4}.sub_hash?({c: 1, b: 2}) => false
  def sub_hash?(hash)
    merge(hash) == self
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_ext-0.1.3 lib/simple_ext/hash/check.rb
simple_ext-0.1.2 lib/simple_ext/hash/check.rb