Class: Hash

Inherits:
Object
  • Object
show all
Extended by:
FatCore::Hash::ClassMethods
Includes:
FatCore::Hash
Defined in:
lib/fat_core/hash.rb

Deletion collapse

Enumerable Extensions collapse

Key Manipulation collapse

Instance Method Details

#delete_with_value(v) ⇒ Hash Originally defined in module FatCore::Hash

Remove from the hash all keys that have values == to given value or that include the given value if the hash has an Enumerable for a value

Examples:

h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
h.delete_with_value(2) #=> { a: 1, c: 3, e: 1 }
h.delete_with_value([1, 3]) #=> { b: 2, d: 2 }

Parameters:

  • v (Object, Enumerable<Object>)

    value to test for

Returns:

  • (Hash)

    hash having entries === v or including v deleted

#each_pair_with_flagsHash Originally defined in module FatCore::Hash

Yield each key-value pair in the Hash together with two boolean flags that indicate whether the item is the first or last item in the Hash.

Examples:

{a: 1, b: 2, c: 3}.each_pair_with_flags do |k, val, first, last|
  print "#{k} => #{val}"
  print " is first" if first
  print " is last" if last
  print " is nothing special" if !first && !last
  print "\n"
end

#=> output:
a => 1 is first
b => 2 is nothing special
c => 3 is last

Returns:

  • (Hash)

    return self

#keys_with_value(val) ⇒ Array<Object> Originally defined in module FatCore::Hash

Return all keys in hash that have a value == to the given value or have an Enumerable value that includes the given value.

Examples:

h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
h.keys_with_value(2) #=> [:b, :d]
h.keys_with_value([1, 3]) #=> [:a, :c, :e]

Parameters:

  • val (Object, Enumerable<Object>)

    value to test for

Returns:

  • (Array<Object>)

    the keys with value or values v

#remap_keys(key_map = {}) ⇒ Hash Originally defined in module FatCore::Hash

Change each key of this Hash to its value in key_map. Keys not appearing in the key_map remain in the result Hash.

Examples:

h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
key_map = { a: 'alpha', b: 'beta' }
h.remap_keys(key_map) #=> {"alpha"=>1, "beta"=>2, :c=>3, :d=>2, :e=>1}

Parameters:

  • key_map (Hash) (defaults to: {})

    hash mapping old keys to new

Returns:

  • (Hash)

    new hash with remapped keys

#replace_keys(new_keys) ⇒ Hash Originally defined in module FatCore::Hash

Change the keys of this Hash to new_keys, an array of keys of the same size as the Array self.keys.

Examples:

h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
nk = [:z, :y, :x, :w, :v]
h.replace_keys(nk) #=> {:z=>1, :y=>2, :x=>3, :w=>2, :v=>1}

Parameters:

  • new_keys (Array<Object>)

    replacement keys

Returns:

Raises:

  • (ArgumentError)

    if new_keys.size != self.keys.size