Sha256: da40c5da2c03784a7dd3392f5712b81fcb14defe867dca6b5cd8db2170028391

Contents?: true

Size: 597 Bytes

Versions: 2

Compression:

Stored size: 597 Bytes

Contents

module Koalemos
  module UniqBy
    def uniq_by(&b)
      b ||= lambda { |x| x }
      inject({}) { |h, e| h[b[e]] ||= e; h }.values
    end
  end
  
  module ::Enumerable
    if method_defined?(:uniq_by)
      warn "#{self}#uniq_by already defined, didn't include at #{__FILE__}:#{__LINE__}"
    else
      include UniqBy
    end
  end
  
  class ::Array
    if method_defined?(:uniq_by)
      warn "#{self}#uniq_by already defined, didn't include at #{__FILE__}:#{__LINE__}"
    else
      include UniqBy
      
      def uniq_by!(&b)
        replace uniq_by(&b)
      end
    end
  end
    
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
koalemos-0.0.2 lib/koalemos/uniq_by.rb
koalemos-0.0.1 lib/koalemos/uniq_by.rb