我的Rails 3.1应用程序中有一个由多个对象组成的数组:
[#<Hardware id: 10, brand_id: 5, model: "B4200", description: "Stampante OKI B4200", typology_id: 3, sub_typology_id: 10, created_at: nil, updated_at: nil>, #<Hardware id: 19, brand_id: 9, model: "JetLab", description: "JetLab - 600 ", typology_id: 5, sub_typology_id: nil, created_at: nil, updated_at: nil>]
Run Code Online (Sandbox Code Playgroud)
我想从这个数组中删除一个对象.使用Rails控制台,我试图做一些事情(尝试删除第一个对象):
array.pop=#<Hardware id: 10, brand_id: 5, model: "B4200", description: "Stampante OKI B4200", typology_id: 3, sub_typology_id: 10, created_at: nil, updated_at: nil>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我怎样才能做到这一点?
更新:我的目标不是在数组上弹出最后一个元素,而是使用mysql搜索查询找到一个通用对象(数组中的任何位置).
my_array = [ 1, 2, 3 ]
item = my_array.pop
puts item
# => 3
puts my_array
# => [ 1, 2 ]
Run Code Online (Sandbox Code Playgroud)
您可能希望使用Array#delete函数
an_array = [1,3,4]
an_array.delete(3)
# => 3
puts an_array
# => [1,4]
Run Code Online (Sandbox Code Playgroud)
在Ruby文档中查看:
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-delete
| 归档时间: |
|
| 查看次数: |
9481 次 |
| 最近记录: |