Module: AssociateRB::ArrayExtension
- Defined in:
- lib/extensions/array_extension.rb
Instance Method Summary collapse
-
#associate {|it| ... } ⇒ Hash
Returns a [Hash] containing key-value pairs provided by the block applied to elements of the given [Array].
-
#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.
-
#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.
-
#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.
-
#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.
-
#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.
Instance Method Details
#associate {|it| ... } ⇒ Hash
Returns a [Hash] containing key-value pairs provided by the block applied to elements of the given [Array].
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.
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.
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.
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.
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.
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 |