Module: FatCore::Hash

Included in:
Hash
Defined in:
lib/fat_core/hash.rb

Overview

It provides a couple of methods for manipulating the keys of a Hash: #remap_keys for translating the current set of keys to a new set provided by a Hash of old to new keys, and #replace_keys for doing a similar operation with an Array of new keys. Along the same line, the method #keys_with_value will return the keys in a Hash equal to the given value of any of an Array of values.

It also provides a method for deleting all entries in a Hash whose value match a single value or any one of an Array of values in #delete_with_value

Finally, it provides an #each_pair-like method, #each_pair_with_flags, that yields each key-value pair of the Hash along with two boolean flags that indicate whether the element is the first or last in the Hash.

Enumerable Extensions collapse

Deletion collapse

Key Manipulation collapse

Instance Method Details

#<<(other) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/fat_core/hash.rb', line 170

def <<(other)
  case other
  when Hash
    merge(other)
  when Enumerable
    merge(other.flatten.each_slice(2).to_h)
  end
end

#delete_with_value(*val) ⇒ Hash

Return a copy of the Hash a Hash with all keys that have values == to the given value or values.

Examples:

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

Parameters:

  • val (Object, Enumerable<Object>)

    value to test for

Returns:

  • (Hash)

    hash having entries === v or including v deleted



99
100
101
102
103
104
105
106
107
# File 'lib/fat_core/hash.rb', line 99

def delete_with_value(*val)
  hsh = clone
  val.each do |v|
    hsh.keys_with_value(v).each do |k|
      hsh.delete(k)
    end
  end
  hsh
end

#delete_with_value!(*val) ⇒ Hash

Remove from the hash in place all keys that have values == to the given value or values.

Examples:

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

Parameters:

  • val (Object, Enumerable<Object>)

    value to test for

Returns:

  • (Hash)

    hash having entries === v or including v deleted



77
78
79
80
81
82
83
84
# File 'lib/fat_core/hash.rb', line 77

def delete_with_value!(*val)
  val.each do |v|
    keys_with_value(v).each do |k|
      delete(k)
    end
  end
  self
end

#each_pair_with_flagsHash

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fat_core/hash.rb', line 51

def each_pair_with_flags
  last_k = size - 1
  k = 0
  each_pair do |key, val|
    first = k.zero?
    last  = (k == last_k)
    yield(key, val, first, last)
    k += 1
  end
  self
end

#keys_with_value(*vals) ⇒ Array<Object>

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



121
122
123
124
125
126
127
128
129
# File 'lib/fat_core/hash.rb', line 121

def keys_with_value(*vals)
  keys = []
  vals.each do |val|
    each_pair do |k, v|
      keys << k if self[k] == val || (v.respond_to?(:include?) && v.include?(val))
    end
  end
  keys
end

#remap_keys(key_map = {}) ⇒ 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



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fat_core/hash.rb', line 141

def remap_keys(key_map = {})
  new_hash = {}
  each_pair do |key, val|
    if key_map.key?(key)
      new_hash[key_map[key]] = val
    else
      new_hash[key] = val
    end
  end
  new_hash
end

#replace_keys(new_keys) ⇒ 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



164
165
166
167
168
# File 'lib/fat_core/hash.rb', line 164

def replace_keys(new_keys)
  raise ArgumentError, 'replace_keys: new keys size differs from key size' unless keys.size == new_keys.size

  to_a.each_with_index.to_h { |(_k, v), i| [new_keys[i], v] }
end