module AssociateRB::ArrayExtension
Public Instance Methods
Returns a [Hash] containing key-value pairs provided by the block applied to elements of the given [Array].
@return [Hash] the association
@yield [it] Executes the association block
# File lib/associate_rb/extensions/array_extension.rb, line 9 def associate associate_to({}) { |it| yield(it) } end
Returns a [Hash] containing the elements from the given [Array] indexed by the key returned from the block applied to each element.
@return [Hash] the association
@yield [it] the value to be associated (becomes the key)
# File lib/associate_rb/extensions/array_extension.rb, line 19 def associate_by associate { |it| yield(it).to it } end
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.
@return [Hash] the association
@yield [it] the value to be associated (becomes the key)
# File lib/associate_rb/extensions/array_extension.rb, line 61 def associate_by_to(association) associate_to(association) { |it| yield(it).to it } end
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.
@param association [Hash] the Hash to merge with the new values @return [Hash] the association
@yield [it] the value to be associated
# File lib/associate_rb/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
Returns a [Hash] where keys are elements from the given [Array] and values are produced by the block applied to each element.
@return [Hash] the association
@yield [it] the value to be associated (becomes the value)
# File lib/associate_rb/extensions/array_extension.rb, line 29 def associate_with associate { |it| it.to yield(it) } end
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.
@return [Hash] the association
@yield [it] the value to be associated (becomes the value)
# File lib/associate_rb/extensions/array_extension.rb, line 72 def associate_with_to(association) associate_to(association) { |it| it.to yield(it) } end