Sha256: adebcecdb641e8b14786cc349e2344aed8560e5309611ab86d65559eebff259b

Contents?: true

Size: 489 Bytes

Versions: 3

Compression:

Stored size: 489 Bytes

Contents

module Enumerable

  # Convert enumerable into a Hash, iterating over each member
  # where the provided block must return the key to by used
  # to map to the value.
  #
  # Examples:
  #
  #   [:a,:b,:c].key_by{ |v| v.to_s }
  #   #=> {'a'=>:a, 'b'=>:b, 'c'=>:c}
  #
  # TODO: How should this method behave with a Hash?
  #
  # Returns: Hash

  def key_by
    return to_enum(:key_by) unless block_given?

    h = {}
    each do |v|
      h[yield(v)] = v
    end

    return h
  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/enumerable/key_by.rb
facets-3.1.0 lib/core/facets/enumerable/key_by.rb
facets-3.0.0 lib/core/facets/enumerable/key_by.rb