Programmingresources Wiki
Advertisement

In the following tutorial, we will learn how to remove elements from arrays in different ways. You can read the documentation of all of these at http://www.ruby-doc.org/core-1.9.3/Array.html

We will be using the array A = [1,2,3,4,5,6] to illustrate the following techniques

Removing the first element of an array

To remove the first element of an array,we need to use Array.shift or Array.shift() command. Here is my example using the Array A.

A.shift() should remove the first element of A which is 1 and it should return A = [2,3,4,5,6]


Arrayshift










Removing the last element of an array

To remove the last element of an array,we can use the Array.pop or Array.pop() command. Here is my example using the Array A

A.pop() should remove the last element of the A which is 6 and it should return A =[1,2,3,4,5]



Arraypop










Remove an element of an array at a given index

If you are unfamiliar with the way indexing works in ruby,please read http://programmingresources.wikia.com/wiki/Indexing_Arrays_in_Ruby before trying out this tutorial.

If you want to remove an element of an array at an index, use Array.delete_at(index) command. It will remove the element at the given index. Here is my example using the array A

I want to remove the element at index 2 which is 3. So I type in A.delete_at(2) which should remove the element at index 2.



Arraydeleteat











Alternatively, you can also use the Array.slice!(index) method to remove elements at a given index. I am repeating the above example using A.slice!(2)




Arrayslice











Remove an element(s) of an array using value of the element(s)

You can also use a value to remove an element from an array. You need to use Array.delete(value) . The command will remove all the elements of the array which matches the value. So be careful with it. If you just want to remove the first occurance of a repeating value, please read the next tutorial. Here is my example using Array A

I need to remove the value 3 from my array. So I call A.delete(3). It will remove remove from A return the rest.



Arraydelete











Alternatively, you can also subtract values from the array to achieve the same thing. Array-[value_to_be_removed] will remove the value from the array and return back the rest of the elements. But there is one catch here. You need to reassign to it the original variable containing array for the operation to take effect. Here is my example using my array A. (You will get a ruby warning that you are assigning to a variable which is already "initialized". But in this example, ignore it. It will not affect the goal here)

I want to remove the value 3 from A. So I write A = A -[3] on the irb and I get back A with 3 removed.


This is the final way and a more advanced way in which you can remove all the elements of which match a given value. We can also use Array.reject{} to remove elements which match a particular value.

Here is my example

I want to remove 3 from the Array A. So I type A.reject{ |k| k==3}. I get the following output in irb


Arrayreject










Remove the first occurance of a repeating element using value of the element

The last tutorial helped you to remove all elements with a repeating value from an array. But there might be situations in which you might want to remove the first occurance of a repeating element. Here I am going to redefine A = [9,2,7,2,3,5]

If I want to remove the first occurance of the value 2 from the array A, I need to use a mixture of two commands.

The command is Array.delete_at(Array.index(value)) will remove the first occurance of a repeating value from the array. I have applied that to A below.


Arraydeleteatarrayindex










I read the tutorial by http://www.peachpit.com/articles/article.aspx?p=1278994&seqNum=4 on removing elements from a ruby array and I was inspired by that to write this. I have written similar examples to theirs and also added more material and screen shots to expand this.

Advertisement