Sha256: e768c0974f993bf6febc70635a91e0d1e1052e4f9271cd92e10452e7d5123074
Contents?: true
Size: 946 Bytes
Versions: 4
Compression:
Stored size: 946 Bytes
Contents
module Enumerable # 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], 4 => [4] } # # ["foo", "bar", "a"].commonality { |str| str.length } # # => { 3 => ["foo, "bar"] } # # # Returns all persons that share their last name with another person. # persons.collisions { |person| person.last_name } # # CREDIT: Florian Gross def commonality( &block ) had_no_block = !block block ||= lambda { |item| item } result = Hash.new { |hash, key| hash[key] = Array.new } self.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 end
Version data entries
4 entries across 4 versions & 2 rubygems