[英]In Rails, how do I figure out if an array of objects contains specific attributes matching given values?
I'm using Ruby on Rails 5.0.1 with Ruby 2.4. 我在Ruby 2.4上使用Ruby on Rails 5.0.1。 I have an array of objects, stored in the array, "results." 我有一个对象数组,存储在数组“结果”中。 Each object has a numeric attribute 每个对象都有一个数字属性
numeric_attr
I would like to know, given my array, how I can tell if I have exactly one object with a numeric attribute value of "1" and incrementing by one. 我想知道,在给定数组的情况下,如何确定我是否只有一个对象的数值属性值为“ 1”并递增1。 Order is not important. 顺序并不重要。 So, for instance, if I have an array of three objects, 因此,例如,如果我有一个由三个对象组成的数组,
[MyObject(numeric_attr = 2), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]
I want to know if I have exactly one object with numeric_attr = 1, another object with numeric_attr = 2, and another with numeric_attr = 3. So the above satisfies the condition. 我想知道是否有一个对象的numeric_attr = 1,另一个对象的numeric_attr = 2,另一个对象的numeric_attr =3。因此,上述条件可以满足。 The below example does not 下面的例子不
[MyObject(numeric_attr = 4), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]
because although there is an object with numeric_attr = 1, there is no object with numeric_attr = 2. It is possible thet the numeric_attr field is nil. 因为尽管有一个对象的numeric_attr = 1,但是没有对象的numeric_attr =2。有可能thenumeric_attr字段为nil。 How can I figure this out? 我该如何解决?
This one-liner should work: 这种单线工作:
results.map(&:numeric_attr).sort == (1..results.count).to_a
Explanation: 说明:
results
#=> [#<MyObject:... @attr=2>, #<MyObject:... @attr=3>, #<MyObject:... @attr=1>]
results.map(&:attr)
#=> [2, 3, 1]
results.map(&:attr).sort
#=> [1, 2, 3]
(1..results.length).to_a
#=> [1, 2, 3]
# therefore:
results.map(&:attr).sort == (1..results.count).to_a
#=> true
If there is a chance that numeric_attr
is nil
: 如果numeric_attr
有可能为nil
:
results.map(&:attr).compact.sort == (1..results.count).to_a
Of course, if there is even a single nil
value, the result is guaranteed to be false
. 当然,即使只有一个nil
值,也保证结果为false
。
If the sequence could start at any number, not just 1: 如果序列可以以任意数字开头,而不仅仅是1:
results.map(&:attr).sort == results.count.times.to_a.
map { |i| i + results.map(&:attr).sort.first }
This is not very efficient though, as it sorts the numbers twice. 但是,这并不是很有效,因为它会将数字排序两次。
If they always start at 1 @Máté's solution works, if they can start at any arbitrary number then you could: 如果它们始终从1开始,@Máté的解决方案有效,如果它们可以从任意数字开始,则可以:
count = 0
array_objects.sort_by(&:numeric_attr).each_cons(2) {|a,b| count+=1 if a.numeric_attr==b.numeric_attr-1 }
count+1==array_objects.count
Not as elegant but handles a lot more situations 不那么优雅,但可以处理更多情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.