Remove item from array

I have an array in my Rails 3.1 applications that made several objects:

[#<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>] 

and I want to remove one object from this array. Using the Rails console, I tried to do something like (try to delete the first object):

 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> 

but that will not work. How can i do this?

UPDATED: My goal is not to call the last element in the array, but a common object (everywhere inside the array), which should be found using the mysql search query.

+7
source share
2 answers
 my_array = [ 1, 2, 3 ] item = my_array.pop puts item # => 3 puts my_array # => [ 1, 2 ] 
+9
source

You probably want to use the Array # delete function

 an_array = [1,3,4] an_array.delete(3) # => 3 puts an_array # => [1,4] 

Check this out in the Ruby documentation:

http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-delete

+6
source

All Articles