Sha256: dc0106d0f834bef2d2fa85e935b6a44e8f8aef679047eaf403f62f0a6b87ce95

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

class Hash

  # 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

  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

  # The in-place version of Hash#revalue.
  #
  #     hash = { a: 1, b: 2 }
  #     hash.revalue! { |v| v + 1 }
  #     hash  # => { a: 2, b: 3 }
  #
  # Returns [Hash].
  #
  # Credit: Sean Mackesey

  def revalue!(val_map=nil, &block)
    replace(revalue(val_map, &block))
  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/hash/revalue.rb
facets-3.1.0 lib/core/facets/hash/revalue.rb
facets-3.0.0 lib/core/facets/hash/revalue.rb