lib/nrser/ext/enumerable/associate.rb
# Convert an enumerable to a hash by passing each entry through `&block` to
# get it's key, raising an error if multiple entries map to the same key.
# 
# @example Basic usage
#   ['a', :b].assoc_by &:class
#   # => {String=>"a", Symbol=>:b}
# 
# @example Conflict error
#   [:a, :b].assoc_by &:class
#   # NRSER::ConflictError: Key Symbol is already in results with value:
#   # 
#   #     :a
#   #
# 
# @param [Proc<(V)=>K>] block
#   Block that maps this {Enumerable}'s values to their hash keys.
# 
# @return [Hash<K, V>]
# 
# @raise [NRSER::ConflictError]
#   If two values map to the same key.
# 
Convert an enumerable to a hash by passing each entry through `█` to
get it's key, raising an error if multiple entries map to the same key.
Convert an enumerable to a hash by passing each entry through ` █ ` to get it ' s key raising an error if multiple entries map to the same key .
def assoc_by &block
    each_with_object( {} ) { |entry, result|
      key = block.call entry
      if result.key? key
        raise NRSER::ConflictError.new \
          "Key", key, "for entry", entry,
          "is already in results pointing to value", result[key],
          entry: entry,
          key: key,
          existing_value: result[ key ]
      end
      result[key] = entry
    }
  end
def assoc_by & block each_with_object ( { } ) { | entry , result | key = block . call entry if result . key? key raise NRSER :: ConflictError . new "Key" , key , "for entry" , entry , "is already in results pointing to value" , result [ key ] , entry : entry , key : key , existing_value : result [ key ] end result [ key ] = entry } end
Convert an enumerable to a hash by passing each entry through `&block` to
get it's key, raising an error if multiple entries map to the same key.
Convert an enumerable to a hash by passing each entry through ` &block ` to get it ' s key raising an error if multiple entries map to the same key .
None
Hash<K, V>
[NRSER::ConflictError] If two values map to the same key.
[ NRSER :: ConflictError ] If two values map to the same key .
block
<not_specific>
<not_specific>
block Block that maps this {Enumerable}'s values to their hash keys.
block Block that maps this { Enumerable } ' s values to their hash keys .
example
None
example
Conflict error [:a, :b].assoc_by &:class # NRSER::ConflictError: Key Symbol is already in results with value: # # :a
Conflict error [ : a : b ] . assoc_by & : class # NRSER :: ConflictError : Key Symbol is already in results with value : # # : a