Method: Array#commonality

Defined in:
lib/core/facets/array/commonality.rb

#commonality(&block) ⇒ Object Also known as: collisions

Returns all items that are equal in terms of the supplied block. If no block is given objects are considered to be equal if they return the same value for Object#hash and if obj1 == obj2.

[1, 2, 2, 3, 4, 4].commonality  #=> { 2 => [2, 2], 4 => [4, 4] }

["foo", "bar", "a"].commonality { |str| str.length }
#=> { 3 => ["foo", "bar"] }

This can be useful, for instance, in determining all persons that share their last name with another person …

persons.collisions { |person| person.last_name }

CREDIT: Florian Gross



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/core/facets/array/commonality.rb', line 19

def commonality(&block)
  had_no_block = !block
  block ||= lambda { |item| item }
  result = Hash.new { |hash, key| hash[key] = Array.new }
  each do |item|
    key = block.call(item)
    result[key] << item
  end
  result.reject! do |key, values|
    values.size <= 1
  end
  # -- return had_no_block ? result.values.flatten : result
  return result
end