Module: FatCore::Hash

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

Enumerable Extensions collapse

Deletion collapse

Key Manipulation collapse

Instance Method Details

#delete_with_value(v) ⇒ 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

[View source]

69
70
71
72
73
74
# File 'lib/fat_core/hash.rb', line 69

def delete_with_value(v)
  keys_with_value(v).each do |k|
    delete(k)
  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

[View source]

45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fat_core/hash.rb', line 45

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

#keys_with_value(val) ⇒ 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

[View source]

88
89
90
91
92
93
94
95
96
# File 'lib/fat_core/hash.rb', line 88

def keys_with_value(val)
  result = []
  each_pair do |k, v|
    if self[k] == val || (v.respond_to?(:include?) && v.include?(val))
      result << k
    end
  end
  result
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

[View source]

108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fat_core/hash.rb', line 108

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

[View source]

131
132
133
134
135
136
# File 'lib/fat_core/hash.rb', line 131

def replace_keys(new_keys)
  unless keys.size == new_keys.size
    raise ArgumentError, 'replace_keys: new keys size differs from key size'
  end
  to_a.each_with_index.map { |(_k, v), i| [new_keys[i], v] }.to_h
end