Sha256: 920613cd308101209cb92df345733afe98593aa984b36c1f896924fc6267ef87
Contents?: true
Size: 548 Bytes
Versions: 15
Compression:
Stored size: 548 Bytes
Contents
# frozen_string_literal: true Hash.class_eval do # Invert a hash keeping possible duplicate values. # @see Hash#invert for more details or to use without keeping duplicates. # @see https://stackoverflow.com/a/10989394/8806642 for source. # # @example # h = {:a => 1, :b => 2, :c => 3, :d => 4, :z => 1} # h.invert # # => {1=>:z, 2=>:b, 3=>:c, 4=>:d} # h.invert_with_dups # # => {1=>[:a, :z], 2=>[:b], 3=>[:c], 4=>[:d]} # # @return [Hash] # def invert_with_dups keys.group_by { |key| self[key] } end end
Version data entries
15 entries across 15 versions & 1 rubygems