Module: AssociateRB::ArrayExtension

Defined in:
lib/extensions/array_extension.rb

Instance Method Summary collapse

Instance Method Details

#associate {|it| ... } ⇒ Hash

Returns a [Hash] containing key-value pairs provided by the block applied to elements of the given [Array].

Yields:

  • (it)

    Executes the association block

Returns:

  • (Hash)

    the association



9
10
11
# File 'lib/extensions/array_extension.rb', line 9

def associate
  associate_to({}) { |it| yield(it) }
end

#associate_by {|it| ... } ⇒ Hash

Returns a [Hash] containing the elements from the given [Array] indexed by the key returned from the block applied to each element.

Yields:

  • (it)

    the value to be associated (becomes the key)

Returns:

  • (Hash)

    the association



19
20
21
# File 'lib/extensions/array_extension.rb', line 19

def associate_by
  associate { |it| yield(it).to it }
end

#associate_by_to(association) {|it| ... } ⇒ Hash

Returns a [Hash] containing the elements from the given [Array] indexed by the key returned from the block applied to each element, and the elements of the [Hash] sent to the method.

Yields:

  • (it)

    the value to be associated (becomes the key)

Returns:

  • (Hash)

    the association



61
62
63
# File 'lib/extensions/array_extension.rb', line 61

def associate_by_to(association)
  associate_to(association) { |it| yield(it).to it }
end

#associate_to(association) {|it| ... } ⇒ Hash

Returns a [Hash] containing key-value pairs provided by the block applied to elements of the given [Array], and the elements of the

Hash

sent to the method.

Parameters:

  • association (Hash)

    the Hash to merge with the new values

Yields:

  • (it)

    the value to be associated

Returns:

  • (Hash)

    the association

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/extensions/array_extension.rb', line 41

def associate_to(association)
  raise ArgumentError, 'No block provided' unless block_given?
  raise ArgumentError, 'Given association is not a Hash' unless association.is_a?(Hash)

  self.each do |it|
    associated = yield(it)
    raise ArgumentError, 'Block does not return Hash' unless associated.is_a?(Hash)

    association.merge!(yield(it))
  end
  association
end

#associate_with {|it| ... } ⇒ Hash

Returns a [Hash] where keys are elements from the given [Array] and values are produced by the block applied to each element.

Yields:

  • (it)

    the value to be associated (becomes the value)

Returns:

  • (Hash)

    the association



29
30
31
# File 'lib/extensions/array_extension.rb', line 29

def associate_with
  associate { |it| it.to yield(it) }
end

#associate_with_to(association) {|it| ... } ⇒ Hash

Returns a [Hash] where keys are elements from the given [Array] and values are produced by the block applied to each element, and the elements of the [Hash] sent to the method.

Yields:

  • (it)

    the value to be associated (becomes the value)

Returns:

  • (Hash)

    the association



72
73
74
# File 'lib/extensions/array_extension.rb', line 72

def associate_with_to(association)
  associate_to(association) { |it| it.to yield(it) }
end