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

On 27 Aug 2009, at 15:09, Patrick D. wrote:

Something like…

records.map(&:description).all?(&:==)

Well if you’re desperate for a one-liner
records.map(&:description).uniq.length == 1

Fred

records.map(&:description).all?(&:==)

nice… I was trying to figure out something with #inject/#sum, but
that looks great.

–wpd

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs