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

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, keys) ⇒ HashBuilder

Returns a new instance of HashBuilder.

Parameters:

  • values (::Array)

    List of values of the hash

  • keys (::Array)

    List of keys of the hash



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

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

Instance Attribute Details

#keysObject

keys of the hash to be built



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/darthjee/core_ext/array/hash_builder.rb', line 28

class HashBuilder
  attr_accessor :values, :keys

  # @param [::Array] values List of values of the hash
  # @param [::Array] keys List of keys of the hash
  def initialize(values, keys)
    @values = values.dup
    @keys = keys.dup
  end

  # Builds the hash
  # @return [::Hash] Hash whose keys and values are paired
  #   from builder's keys and values
  def build
    fixes_sizes

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

  private

  def fixes_sizes
    return unless needs_resizing?
    values.concat ::Array.new(keys.size - values.size)
  end

  def needs_resizing?
    keys.size > values.size
  end
end

#valuesObject

values of the hash to be built



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/darthjee/core_ext/array/hash_builder.rb', line 28

class HashBuilder
  attr_accessor :values, :keys

  # @param [::Array] values List of values of the hash
  # @param [::Array] keys List of keys of the hash
  def initialize(values, keys)
    @values = values.dup
    @keys = keys.dup
  end

  # Builds the hash
  # @return [::Hash] Hash whose keys and values are paired
  #   from builder's keys and values
  def build
    fixes_sizes

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

  private

  def fixes_sizes
    return unless needs_resizing?
    values.concat ::Array.new(keys.size - values.size)
  end

  def needs_resizing?
    keys.size > values.size
  end
end

Instance Method Details

#build::Hash

Builds the hash

Returns:

  • (::Hash)

    Hash whose keys and values are paired from builder’s keys and values



41
42
43
44
45
# File 'lib/darthjee/core_ext/array/hash_builder.rb', line 41

def build
  fixes_sizes

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