Hi all. Why Array#uniq doesn''t take block? I think it''s nice if we can pass block. I know it''s enough when object is simple like Fixnum. [1,2,2,3].uniq #=> [1,2,3] but when it comes to complex object like ActiveRecord model Company.all.uniq { |c| c.name } I found a patch (https://rails.lighthouseapp.com/projects/8994/tickets/ 1595-patch-for-activesupport-that-arrayuniq-and-uniq-supports-blocks) that enables this feature but it was rejected unfortunately. Anyone have an idea why it''s rejected? Thanks. -- 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 26 December 2010 20:32, Shou <pettion@gmail.com> wrote:> Hi all. > > Why Array#uniq doesn''t take block? I think it''s nice if we can pass > block. > I know it''s enough when object is simple like Fixnum. > > [1,2,2,3].uniq #=> [1,2,3] > > but when it comes to complex object like ActiveRecord model > > Company.all.uniq { |c| c.name }How would it know which record to keep and which to reject when it found two entries with the same name? Colin -- 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 26 December 2010 21:48, Colin Law <clanlaw@googlemail.com> wrote:> On 26 December 2010 20:32, Shou <pettion@gmail.com> wrote: >> Hi all. >> >> Why Array#uniq doesn''t take block? I think it''s nice if we can pass >> block. >> I know it''s enough when object is simple like Fixnum. >> >> [1,2,2,3].uniq #=> [1,2,3] >> >> but when it comes to complex object like ActiveRecord model >> >> Company.all.uniq { |c| c.name } > > How would it know which record to keep and which to reject when it > found two entries with the same name?Or perhaps I misunderstand, perhaps you just mean Company.all.map(&:name).uniq Colin -- 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 Sun, Dec 26, 2010 at 09:52:29PM +0000, Colin Law wrote:> On 26 December 2010 21:48, Colin Law <clanlaw@googlemail.com> wrote: > > On 26 December 2010 20:32, Shou <pettion@gmail.com> wrote: > >> Hi all. > >> > >> Why Array#uniq doesn''t take block? I think it''s nice if we can pass > >> block. > >> I know it''s enough when object is simple like Fixnum. > >> > >> [1,2,2,3].uniq #=> [1,2,3] > >> > >> but when it comes to complex object like ActiveRecord model > >> > >> Company.all.uniq { |c| c.name } > > > > How would it know which record to keep and which to reject when it > > found two entries with the same name? > > Or perhaps I misunderstand, perhaps you just mean > Company.all.map(&:name).uniqSounds like he wants uniq_by: http://redcorundum.blogspot.com/2007/02/have-you-seen-that-key.html> Colin--Greg -- 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 Sun, Dec 26, 2010 at 9:32 PM, Shou <pettion@gmail.com> wrote:> Why Array#uniq doesn''t take block? I think it''s nice if we can pass > block. > I know it''s enough when object is simple like Fixnum. > > [1,2,2,3].uniq #=> [1,2,3] > > but when it comes to complex object like ActiveRecord model > > Company.all.uniq { |c| c.name }Apart from the merits of the idiom itself, generally speaking Active Support extends Ruby mostly to ease the development of Rails itself, and only adds other core extensions when they are clearly of benefit for almost any web application. That''s the rule of thumb, it doesn''t mean you won''t find a counterxample, but it''s what guides additions. In that sense Active Support has a different goal than Facets, for example. -- 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.
>> CollinCompany.all.map(&:name).uniq returns an array of strings but what I want here is an array of company objects. So that''s not what I want here.>> XavierI under the idea of core extensions.>> GregoryThat''s exactly what I wanted. FYI, I didn''t realize but Array#uniq_by in core_ext does the trick. Thank you all for your quick reply! Shou On Dec 27, 6:53 am, Gregory Seidman <gsslist +railsc...@anthropohedron.net> wrote:> On Sun, Dec 26, 2010 at 09:52:29PM +0000, Colin Law wrote: > > On 26 December 2010 21:48, Colin Law <clan...@googlemail.com> wrote: > > > On 26 December 2010 20:32, Shou <pett...@gmail.com> wrote: > > >> Hi all. > > > >> Why Array#uniq doesn''t take block? I think it''s nice if we can pass > > >> block. > > >> I know it''s enough when object is simple like Fixnum. > > > >> [1,2,2,3].uniq #=> [1,2,3] > > > >> but when it comes to complex object like ActiveRecord model > > > >> Company.all.uniq { |c| c.name } > > > > How would it know which record to keep and which to reject when it > > > found two entries with the same name? > > > Or perhaps I misunderstand, perhaps you just mean > > Company.all.map(&:name).uniq > > Sounds like he wants uniq_by:http://redcorundum.blogspot.com/2007/02/have-you-seen-that-key.html > > > Colin > > --Greg-- 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.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 12/26/2010 06:11 PM, Shou wrote:>>> Collin > Company.all.map(&:name).uniq returns an array of strings but what I > want here is an array of company objects. So that''s not what I want > here. > >>> Xavier > I under the idea of core extensions. > >>> Gregory > That''s exactly what I wanted. FYI, I didn''t realize but Array#uniq_by > in core_ext does the trick. > > Thank you all for your quick reply! > > Shou > > On Dec 27, 6:53 am, Gregory Seidman <gsslist > +railsc...@anthropohedron.net> wrote: >> On Sun, Dec 26, 2010 at 09:52:29PM +0000, Colin Law wrote: >>> On 26 December 2010 21:48, Colin Law <clan...@googlemail.com> wrote: >>>> On 26 December 2010 20:32, Shou <pett...@gmail.com> wrote: >>>>> Hi all. >> >>>>> Why Array#uniq doesn''t take block? I think it''s nice if we can pass >>>>> block. >>>>> I know it''s enough when object is simple like Fixnum. >> >>>>> [1,2,2,3].uniq #=> [1,2,3] >> >>>>> but when it comes to complex object like ActiveRecord model >> >>>>> Company.all.uniq { |c| c.name } >> >>>> How would it know which record to keep and which to reject when it >>>> found two entries with the same name? >> >>> Or perhaps I misunderstand, perhaps you just mean >>> Company.all.map(&:name).uniq >> >> Sounds like he wants uniq_by:http://redcorundum.blogspot.com/2007/02/have-you-seen-that-key.html >> >>> Colin >> >> --Greg >Or uniq on 1.9.2 ;) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk0qhUYACgkQmXMNXIqaMIStewCcDgD82Gqi66NTcMazjhZ30Am3 d5AAn0BzW3GLVQPY5YyZwqUmmZc8fKIx =tYv0 -----END PGP SIGNATURE----- -- 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.