Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/ury_rapid/common/hash.rb

Overview

Reopening of the Hash class to add utility functions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_with_default_block(contents, &block) ⇒ Hash

Creates a new Hash with the given contents and default block

Parameters:

  • contents (Hash)

    A hash containing the desired initial mapping.

Returns:

  • (Hash)

    A new hash with the given contents and default block.


29
30
31
# File 'lib/ury_rapid/common/hash.rb', line 29

def self.new_with_default_block(contents, &block)
  new(&block).merge!(contents)
end

Instance Method Details

#map_to_hashHash

Like Hash#map, but returns a new Hash.

Yields the key and value (expects a new key and value back).

Returns:

  • (Hash)

    The new, transformed Hash.


8
9
10
11
12
13
# File 'lib/ury_rapid/common/hash.rb', line 8

def map_to_hash
  each_with_object({}) do |(key, value), hash|
    new_key, new_value = yield(key, value)
    hash[new_key] = new_value
  end
end

#transform_valuesObject

Creates a new Hash by transforming the values of this Hash.

Yields each value (expects a new value back).


20
21
22
# File 'lib/ury_rapid/common/hash.rb', line 20

def transform_values
  map_to_hash { |key, value| [key, yield(value)] }
end