lib/nrser/ext/enumerable/associate.rb
# Create a {Hash} mapping the entries in this {Enumerable} to the value
# returned by passing them through `&block`, raising on conflicts.
# 
# @param [ :raise | :first_wins | :last_wins | Proc ] on_conflict
#   What to do when there's a conflict mapping the entries into the hash.
#   
#   The names are meant to make some sense.
# 
# @param [Proc<(ENTRY)=>VALUE>] block
#   The star of the show! Maps `ENTRY` from this {Enumerable} to its `VALUE`
#   in the resulting hash.
# 
# @return [Hash<ENTRY, VALUE>]
# 
# @raise [NRSER::ConflictError]
#   If a conflict occurs and `on_conflict` is set to `:raise`.
# 
Create a {Hash} mapping the entries in this {Enumerable} to the value
returned by passing them through `█`, raising on conflicts.
Create a { Hash } mapping the entries in this { Enumerable } to the value returned by passing them through ` █ ` raising on conflicts .
def assoc_to on_conflict: :raise, &block
    each_with_object( {} ) { |entry, hash|
      value = if hash.key? entry
        case on_conflict
        when :raise
          raise NRSER::ConflictError.new \
            "Entry", entry, "appears more than once in {Enumerable}",
            details: "This would cause conflict in the resulting {Hash}.",
            entry: entry,
            hash: hash
        when :first_wins
        when :last_wins
          hash[entry] = block.call entry
        when Proc
          hash[entry] = on_conflict.call \
            entry: entry,
            current_value: hash[entry],
            block: block
        else
          raise ArgumentError,
            "Bad `on_conflict`: #{ on_conflict.inspect }"
        end
      else
        block.call entry
      end
      hash[entry] = value
    }
  end
def assoc_to on_conflict : :raise , & block each_with_object ( { } ) { | entry , hash | value = if hash . key? entry case on_conflict when :raise raise NRSER :: ConflictError . new "Entry" , entry , "appears more than once in {Enumerable}" , details : "This would cause conflict in the resulting {Hash}." , entry : entry , hash : hash when :first_wins when :last_wins hash [ entry ] = block . call entry when Proc hash [ entry ] = on_conflict . call entry : entry , current_value : hash [ entry ] , block : block else raise ArgumentError , "Bad `on_conflict`: #{ on_conflict.inspect }" end else block . call entry end hash [ entry ] = value } end
Create a {Hash} mapping the entries in this {Enumerable} to the value
returned by passing them through `&block`, raising on conflicts.
Create a { Hash } mapping the entries in this { Enumerable } to the value returned by passing them through ` &block ` raising on conflicts .
# do nothing
None
Hash<ENTRY, VALUE>
[NRSER::ConflictError] If a conflict occurs and `on_conflict` is set to `:raise`.
[ NRSER :: ConflictError ] If a conflict occurs and ` on_conflict ` is set to ` : raise ` .
on_conflict
<not_specific>
block
<not_specific>
<not_specific>
block The star of the show. Maps `ENTRY` from this {Enumerable} to its `VALUE` in the resulting hash.
block The star of the show . Maps ` ENTRY ` from this { Enumerable } to its ` VALUE ` in the resulting hash .