Method: Hash#revalue

Defined in:
lib/core/facets/hash/revalue.rb

#revalue(val_map = nil, &block) ⇒ Object

Generates a new hash where the values are the result of the passed in block. The block takes both the key and value of the current entry as arguments.

hash = { a: 1, b: 2 }
hash.revalue { |v| v + 1 }  # => { a: 2, b: 3 }

Returns [Hash].

Credit: Sean Mackesey

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/core/facets/hash/revalue.rb', line 14

def revalue(val_map=nil, &block)
  raise ArgumentError, "argument or block, not both" if val_map && block

  if !(val_map or block)
    raise ArgumentError, "must provide Hash arguments or a block"
    #block = lambda{|v| v.to_s}
  end

  if block
    hash = dup.clear  # to keep default_proc
    if block.arity.abs == 1
      each_pair do |k, v|
        hash[k] = block[v]     #hash[k] = block[v] || v
      end
    else
      each_pair do |k, v|
        hash[k] = block[k,v]   #hash[k] = block[k,v] || v
      end
    end
  else
    hash = dup.clear  # to keep default_proc
    each do |k,v|
      if val_map.key?(v)
        hash[k] = val_map[v]
      else
        hash[k] = v
      end
    end
  end

  hash
end