3
\$\begingroup\$

I have an array of active record objects. All objects have an attribute result and the value of result can be either 'failed' or 'passed'.

My goal is to find out if all result attributes are identical. If not then I will have to return mixed but if they are then I return the value of the attributes.

I currently have this code:

if tests.map(&:result).uniq.count != 1
 'mixed'
else
 tests.first.result
end

But that first line is quite ugly. I'm sure there is a better way to find out if all results are identical or not but I can't think of any.

asked Jul 29, 2016 at 1:38
\$\endgroup\$
1
  • \$\begingroup\$ Is it ok that tests = [] returns 'mixed'? \$\endgroup\$ Commented Jul 29, 2016 at 8:23

2 Answers 2

2
\$\begingroup\$

You could try the appropriately named #all? method:

reference = tests.first.result
if tests.all? { |test| test.result == reference }
 reference
else
 'mixed'
end

Edit: As tokland points out, you'll want to check if tests is empty before any of this. Otherwise, tests.first will of course be nil.

But you could also do the check in the database layer with something like:

if Test.select(:result).distinct.count > 1
 'mixed'
else
 Test.first.try(:result)
end

Obviously you'll want to scope the query more specifically.

Also note that here, an empty collection returns a nil (because if Test.first is nil, then .try(:result) will also be nil).

answered Jul 29, 2016 at 4:44
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Note that this fails when tests = [] \$\endgroup\$ Commented Jul 29, 2016 at 8:40
  • \$\begingroup\$ @tokland Hm, yeah, good point... \$\endgroup\$ Commented Jul 29, 2016 at 8:42
2
\$\begingroup\$

Your code looks pretty to me. Note that there is a more efficient way to check equality for all elements in an enumerable: xs.each_cons(2).all? { |x, y| x == y }. I'd write:

results = tests.map(&:result)
if results.present? && results.each_cons(2).all? { |r1, r2| r1 == r2 }
 results.first
else
 'mixed'
end
answered Jul 29, 2016 at 8:32
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.