Sha256: 465d002ebcbcd872dfed23405c6b0a7430ba9f15bf043fe7b9400c3eac3a9bee

Contents?: true

Size: 967 Bytes

Versions: 7

Compression:

Stored size: 967 Bytes

Contents

class Hash

  # Returns true or false whether the hash
  # contains the given keys.
  #
  #   h = { :a => 1, :b => 2 }
  #   h.has_keys?( :a )   #=> true
  #   h.has_keys?( :c )   #=> false
  #
  #   CREDIT: Trans

  def has_keys?(*check_keys)
    unknown_keys = check_keys - self.keys
    return unknown_keys.empty?
  end

  alias_method :keys?, :has_keys?

  # Returns true if the hash contains
  # _only_ the given keys, otherwise false.
  #
  #   h = { :a => 1, :b => 2 }
  #   h.has_only_keys?( :a, :b )   #=> true
  #   h.has_only_keys?( :a )       #=> false
  #
  #   CREDIT: Trans

  def has_only_keys?(*check_keys)
    unknown_keys = self.keys - check_keys
    return unknown_keys.empty?
  end

  alias_method :only_keys?, :has_only_keys?

  # Each with key is like each_pair but reverses the order
  # the parameters to [value,key] instead of [key,value].
  #
  #  CREDIT: Trans

  def each_with_key( &yld )
    each_pair{ |k,v| yld.call(v,k) }
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/hash/keys.rb
facets-2.4.1 lib/facets/hash/keys.rb
facets-2.4.2 lib/core/facets/hash/keys.rb
facets-2.4.3 lib/core/facets/hash/keys.rb
facets-2.4.4 lib/core/facets/hash/keys.rb
facets-2.4.5 lib/core/facets/hash/keys.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/hash/keys.rb