Ruby 如何检查数组中是否存在对象的ID?

Ruby 如何检查数组中是否存在对象的ID?,ruby,arrays,ruby-on-rails-3,logic,Ruby,Arrays,Ruby On Rails 3,Logic,以下是我试图编写的逻辑,但我找不到合适的Ruby方式来表达: if Object.id [occurs in this array ->] [13, 16, 234] #run this code if true else #run this code if false end 基本上,如果id出现在特定数组中的某个位置,我希望返回true。我想您正在寻找: 希望有帮助 使用Arrayinclude? 实现这一点的一种方法是改变逻辑并使用该方法查询数组(而不是实例对象)以供包

以下是我试图编写的逻辑,但我找不到合适的Ruby方式来表达:

if Object.id [occurs in this array ->] [13, 16, 234]
   #run this code if true
else
   #run this code if false
end

基本上,如果id出现在特定数组中的某个位置,我希望返回true。

我想您正在寻找:

希望有帮助

使用Arrayinclude? 实现这一点的一种方法是改变逻辑并使用该方法查询数组(而不是实例对象)以供包含。例如:

[13, 16, 234].include? my_object.id
这将返回一个布尔值,您可以将其插入分支逻辑

[13, 16, 234].include? my_object.id