Class: Darthjee::CoreExt::Array::HashBuilder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/darthjee/core_ext/array/hash_builder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class responsible for building a Hash from 2 arrays

Examples:

Building the hash from the array

values = [10, 20, 30]
keys   = %i[a b c]
builder = Darthjee::CoreExt::Array::HashBuilder.new(values, keys)

builder.build  # returns { a: 10, b: 20, c: 30 }

Rebuilding a hash from values and keys

hash = { a: 20, b: 200, c: 2000 }
builder = Darthjee::CoreExt::Array::HashBuilder.new(
  hash.values,
  hash.keys
)

builder.build == hash   # returns true

Author:

  • Darthjee

Instance Method Summary collapse

Constructor Details

#initialize(values, keys) ⇒ HashBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of HashBuilder.

Parameters:

  • values (::Array)

    List of values of the hash

  • keys (::Array)

    List of keys of the hash



34
35
36
37
# File 'lib/darthjee/core_ext/array/hash_builder.rb', line 34

def initialize(values, keys)
  @values = values.dup
  @keys = keys.dup
end

Instance Method Details

#build::Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds the hash

The building happens through the pairing of keys and values

Returns:



45
46
47
48
49
# File 'lib/darthjee/core_ext/array/hash_builder.rb', line 45

def build
  fixes_sizes

  ::Hash[[keys, values].transpose]
end