Method: Transproc::ArrayTransformations.insert_key

Defined in:
lib/transproc/array.rb

.insert_key(array, key) ⇒ Array

Wraps every value of the array to tuple with given key

The transformation partially inverses the extract_key.

Examples:

fn = t(:insert_key, 'name')
fn.call ['Alice', 'Bob', nil]
# => [{ 'name' => 'Alice' }, { 'name' => 'Bob' }, { 'name' => nil }]

Parameters:

  • array (Array<Hash>)

    The input array of hashes

  • key (Object)

    The key to extract values by

Returns:

  • (Array)


206
207
208
# File 'lib/transproc/array.rb', line 206

def self.insert_key(array, key)
  map_array(array, ->(v) { { key => v } })
end