Skip to main content
deleted 67 characters in body
Source Link
bl0b
  • 405
  • 4
  • 9

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).inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}.firstuniq.secondcount != tests.count1
   '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.

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).inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}.first.second != tests.count
   '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.

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.

added 2 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

So I have an array of active record objects. All 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).inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}.first.second != tests.count
   '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.

So 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).inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}.first.second != tests.count
   '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.

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).inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}.first.second != tests.count
   '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.

Source Link
bl0b
  • 405
  • 4
  • 9

Find out if attributes are identical

So 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).inject(Hash.new(0)) { |total, e| total[e] += 1 ;total}.first.second != tests.count
   '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.