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. Unit test and doc are included. Please test this useful tiny patch. https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6588-add-enumerableuniq Github fork / branch is available at https://github.com/Bounga/rails/commits/enumerable_uniq Have a nice day. -- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
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. On Mar 16, 7:42 am, Nicolas Cavigneaux <n...@bounga.org> 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. > > Unit test and doc are included. > > Please test this useful tiny patch. > > https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6... > > Github fork / branch is available athttps://github.com/Bounga/rails/commits/enumerable_uniq > > Have a nice day. > -- > Nicolas Cavigneauxhttp://www.bounga.orghttp://www.cavigneaux.net-- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
From documentation: enum.one? [{|obj| block }] → true or false 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. Robert Pankowecki -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Le 16 mars 2011 à 17:26, Matt Jones a écrit :> > 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 }How would you handle simple cases like: [ 1, 1, 2 ].uniq? { |x| x > 1 } # => true ? -- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Le 16 mars 2011 à 17:12, Robert Pankowecki a écrit :> From documentation: > > enum.one? [{|obj| block }] → true or falseYes but how to have @post.uniq? { |x| x.created_at.month } ? -- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Hi Chris, I want to be able to do things like: [ 1, 2 ].uniq? [ {:lang => "Ruby"}, {:lang => "ObjC"} ].uniq? [ 1, 1, 2 ].uniq? { |x| x > 1 } [ nc,dhh ].uniq? { |p| p.name } 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. Le 16 mars 2011 à 16:32, Farski a écrit :> 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.-- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
On Wed, Mar 16, 2011 at 8:55 PM, Nicolas Cavigneaux <nicolas@cavigneaux.net> wrote:> Hi Chris, > > I want to be able to do things like: > > [ 1, 2 ].uniq? > [ {:lang => "Ruby"}, {:lang => "ObjC"} ].uniq? > [ 1, 1, 2 ].uniq? { |x| x > 1 } > [ nc,dhh ].uniq? { |p| p.name }Is it possible ? I think you sometimes want to select records using the given proc and sometimes map them. Your current implementation uses "select": class Person < Struct.new(:name, :age) end nc = Person.new(:nc, 1) dhh1 = Person.new(:dhh, 2) dhh2 = Person.new(:dhh, 3) [ nc,dhh1 ].uniq? { |p| p.name } => true [ nc, dhh1, dhh2 ].uniq? { |p| p.name } => true 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... [ 1, 1, 2].uniq? { |x| x > 1 } => true [ 1, 1, 2, 2].uniq? { |x| x > 1 } => false Works as you want. If we change the implementation then first example will go ok but the second one not. We would have to have two methods. Am I right ? uniq_by?(&:name) sounds good for me. Robert Pankowecki -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
[ 1, 1, 2 ].uniq? { |x| x > 1 } # select Could mean: Is it true that all objects greater than 1 are unique ?> [ nc,dhh ].uniq_by? { |p| p.name } # mapCould mean: Is it true that all objects have unique names ? But I am not in favor of adding those methods. Just trying to understand your scenarios. Robert Pankowecki -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Le 16 mars 2011 à 22:55, Robert Pankowecki a écrit :> [ 1, 1, 2 ].uniq? { |x| x > 1 } # select > Could mean: Is it true that all objects greater than 1 are unique ? > >> [ nc,dhh ].uniq_by? { |p| p.name } # map > Could mean: Is it true that all objects have unique names ? > > But I am not in favor of adding those methods. Just trying to > understand your scenarios.Yes it''s what I was trying to do. The only is to add 2 methods. Method names you chose sound good to me. Why don''t you want these methods to be added? -- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Filtering duplicate values should be done in the database layer IMO. On Wed, Mar 16, 2011 at 12:04 PM, Nicolas Cavigneaux <nicolas@cavigneaux.net> wrote:> > Le 16 mars 2011 à 22:55, Robert Pankowecki a écrit : > >> [ 1, 1, 2 ].uniq? { |x| x > 1 } # select >> Could mean: Is it true that all objects greater than 1 are unique ? >> >>> [ nc,dhh ].uniq_by? { |p| p.name } # map >> Could mean: Is it true that all objects have unique names ? >> >> But I am not in favor of adding those methods. Just trying to >> understand your scenarios. > > Yes it''s what I was trying to do. The only is to add 2 methods. Method names you chose sound good to me. > > Why don''t you want these methods to be added? > -- > Nicolas Cavigneaux > http://www.bounga.org > http://www.cavigneaux.net > > > > -- > 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 rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > >-- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
> Why don''t you want these methods to be added?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 ? Robert Pankowecki -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Le 16 mars 2011 à 23:10, Andrew Kaspick a écrit :> Filtering duplicate values should be done in the database layer IMO.The idea here is not to work on DB objects only but on any kind of object. -- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Le 16 mars 2011 à 23:15, Robert Pankowecki a écrit :>> Why don''t you want these methods to be added? > > 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. Thanks a lot for your advices Robert. -- Nicolas Cavigneaux http://www.bounga.org http://www.cavigneaux.net -- 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 rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.