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 name #uniq? here is not a good representation of the method's
functionality here. You're checking for uniqueness of a single member
of the collection, but in use the context would be the entire
collection. @people.uniq? being true should mean @people itself is, in
some way, unique. You'd need to come up with a better way of saying
@people.has_only_one_of?; it may be tough to come up with an
intuitive, succinct name for that, and using something long doesn't
make too much sense when @people.select{}.size==1 is already pretty
accessible.
Passes each element of the collection to the given block. The method
returns true if the block returns true exactly once. If the block is
not given, one? will return true only if exactly one of the collection
members is true.
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...
uniq? seems to be a good name to me. Enumerable#uniq is a good name and Enumerable#uniq? is just the same except we are asking, not really removing duplicates.
All records are selected because p.name is evaluated to true, the
their uniquness is checked which is still true despite the fact the
their all have the same name...
a) Never needed them
b) Rails already extends a lot of things and I see no reason to extend
more and more even if we like it. And I like those methods
c) Because you can extremely easily create a gem with those methods
which then can be easily used by others. Post info on reddit/ruby,
send link it to people creating podcasts about ruby and thousands of
people will know about it and be able to choose if they like it and
want to use it.
Also: Are you sure that such method is not already in facets library ?
Maybe that's a good place to add it ?
a) Never needed them
b) Rails already extends a lot of things and I see no reason to extend
more and more even if we like it. And I like those methods
That's true. We can't include everything in AS
c) Because you can extremely easily create a gem with those methods
which then can be easily used by others. Post info on reddit/ruby,
send link it to people creating podcasts about ruby and thousands of
people will know about it and be able to choose if they like it and
want to use it.
Good idea. It's a nice way to share this.
Also: Are you sure that such method is not already in facets library ?
Wasn't aware of this gem! Thank you Robert.
There's no such methods in Facets.
Maybe that's a good place to add it ?
Yes, sounds like a good place for these methods. Gonna send a patch.