On Mar 16, 2011, at 7:42 AM, Nicolas Cavigneaux wrote:
> Hello !
>
> I wrote an ActiveSupport patch to add a new feature.
>
> This is a tiny tested patch that checks for Enumerable content uniqueness.
>
> It returns true if the collection has no duplicated content and can be called
> with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns
> true if only 1 person is over 26.
The function of the block here seems a little unclear; at first reading I
assumed it was similar to the block passed to sort_by, which specifies *what*
to sort by. As implemented in the patch, though, it's just selecting a subset
of the array.
This implementation might be more useful (in the block_given? path):
uniq_by(&block).size == size
The result would be that the above example would still work, but you could also
say things like:
@some_models.uniq? { |x| x.created_at.month }
to see if an array of models were all created on different months.
That said, I'm not clear on what this would actually be useful for outside of
toy examples...
--Matt Jones
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.