lib/koalemos/uniq_by.rb in koalemos-0.0.2 vs lib/koalemos/uniq_by.rb in koalemos-0.0.3
- old
+ new
@@ -1,21 +1,27 @@
-module Koalemos
+module Koalemos # :nodoc:
module UniqBy
+ # Finds unique values in an array based on a block passed
+ # [ {:key => 'foo'},
+ # {:key => 'foo'},
+ # {:key => 'bar'},
+ # {:key => 'baz'},
+ # {:key => 'bbaz'}].uniq_by{|a| a[:key]} #=> [{:key=>"bbaz"}, {:key=>"baz"}, {:key=>"foo"}, {:key=>"bar"}]
def uniq_by(&b)
b ||= lambda { |x| x }
inject({}) { |h, e| h[b[e]] ||= e; h }.values
end
end
- module ::Enumerable
+ module ::Enumerable# :nodoc:
if method_defined?(:uniq_by)
warn "#{self}#uniq_by already defined, didn't include at #{__FILE__}:#{__LINE__}"
else
include UniqBy
end
end
- class ::Array
+ class ::Array # :nodoc:
if method_defined?(:uniq_by)
warn "#{self}#uniq_by already defined, didn't include at #{__FILE__}:#{__LINE__}"
else
include UniqBy
\ No newline at end of file