【发布时间】:2010-07-27 13:01:20
【问题描述】:
我有一个数组 @horses = [],我用一些随机的马填充。
如何检查我的@horses 数组中是否包含已经包含(存在)的马?
我尝试了类似的方法:
@suggested_horses = []
@suggested_horses << Horse.find(:first,:offset=>rand(Horse.count))
while @suggested_horses.length < 8
horse = Horse.find(:first,:offset=>rand(Horse.count))
unless @suggested_horses.exists?(horse.id)
@suggested_horses<< horse
end
end
我也试过include?,但我发现它只适用于字符串。使用exists? 我收到以下错误:
undefined method `exists?' for #<Array:0xc11c0b8>
所以问题是如何检查我的数组是否已经包含“马”,这样我就不会用同一匹马填充它?
【问题讨论】:
-
如果这个问题不是用 Python 来表达的,那么这个问题将与 stackoverflow.com/questions/1529986/… 重复。
标签: ruby-on-rails ruby