Patrick Doyle
2009-Aug-27 14:09 UTC
Do a set of records have the same value for one of the fields?
Hi Folks, I''m looking for an elegant Ruby one-liner... I have a set of records in my database that I want to check to see if the :description field is identical. And, because it''s Thursday morning (where I live), I would like to do it as elegantly and as one-line-er-ly as I can Something like... records.map(&:description).all?(&:==) which, doesn''t work I''ll go ahead and split it into a couple of lines, and make sure that all of the descriptions match record[0].descriptions, but I am certain there is a more elegant solution than this. --wpd
Frederick Cheung
2009-Aug-27 14:31 UTC
Re: Do a set of records have the same value for one of the fields?
On 27 Aug 2009, at 15:09, Patrick Doyle wrote:> > Hi Folks, > I''m looking for an elegant Ruby one-liner... > > I have a set of records in my database that I want to check to see if > the :description field is identical. > > And, because it''s Thursday morning (where I live), I would like to do > it as elegantly and as one-line-er-ly as I can > > Something like... > > records.map(&:description).all?(&:==) >Well if you''re desperate for a one-liner records.map(&:description).uniq.length == 1 Fred> which, doesn''t work > > I''ll go ahead and split it into a couple of lines, and make sure that > all of the descriptions match record[0].descriptions, but I am certain > there is a more elegant solution than this. > > --wpd > > >