Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the Array.
It can be done with the delete() Method.
x = ["Mohan", "Kriti", "Salim"] x.delete("Kriti") puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the delete() Method that searches for the name Kriti and removes it from the Array.
x.delete("Kriti")
And the new array looks like,
['Mohan', 'Salim']
Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to remove the element at index/position 2 from the Array.
So, we can use del keyword or the pop() Method to remove an element from the Array.
Let us see the example with del keyword first.
x = ["Mohan", "Kriti", "Salim"] x.delete_at(2) puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the del keyword that searches for the element at index/position 2 and removes it from the Array.
x.delete_at(2)
And as we can see, there is Salim at index/position 2. So Salim is removed from the Array.
And the new array is,
['Mohan', 'Kriti']
The shift() Method can be used to remove an item from the end of the Array. If no value is specified at the parameter of shift() Method then it removes the first element by default.
x = ["Mohan", "Kriti", "Salim"] x.shift() puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the shift() Method that searches for the first value by default(As parameter value is specified) and removes it from the Array.
x.shift()
And as we can see, there is Mohan at the front of the Array. So Mohan is removed from the Array.
And the new array is,
["Kriti", "Salim"]
Again the shift() Method can be used to remove any number of items from the end of the Array.
Just specify the number of elements to be removed from the end inside the parameter of shift() method.
x = ["Mohan", "Kriti", "Salim"] x.shift(2) puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the shift(2) Method with parameter 2 that searches for the first 2 values from the array (i.e. Mohan and Kriti) and removes from the array.
x.pop(2)
And as we can see, there is Mohan and Kriti at the front of the Array. So, Mohan and Kriti is removed from the Array.
And the new array is,
['Salim']
The pop() Method can be used to remove an item from the end of the Array. If no value is specified at the parameter of pop(() Method then it removes the last element by default.
x = ["Mohan", "Kriti", "Salim"] x.pop() puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the pop() Method that searches for the last value by default(As no index is specified) and removes it from the Array.
x.pop()
And as we can see, there is Salim at the end of the Array. So Salim is removed from the Array.
And the new array is,
['Mohan', 'Kriti']
Again the pop() Method can be used to remove any number of items from the end of the Array.
Just specify the number of elements to be removed from the end inside the parameter of pop(() method.
x = ["Mohan", "Kriti", "Salim"] x.pop(2) puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the pop(2) Method with parameter 2 that searches for the last 2 values from the array (i.e. Kriti and Salim) and removes from the array.
x.pop(2)
And as we can see, there is Kriti and Salim at the end of the Array. So, Kriti and Salim is removed from the Array.
And the new array is,
['Mohan']
The clear() Method can be used to remove all the elements from the Array.
x = ["Mohan", "Kriti", "Salim"] x.clear() puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Below is how the values are positioned in the Array,
Next, we have used the clear() Method that removes all the elements from the Array making the Array empty.
And we get an empty Array as output,
[]
The uniq() Method can be used to remove duplicate elements from the Array.
x = ["Mohan", "Kriti", "Salim", "Mohan", "Kriti", "Mohan"] y = x.uniq() puts y
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim", "Mohan", "Kriti", "Mohan"]
Below is how the values are positioned in the Array,
And as we can see, Mohan is present 3 times and Kriti is present 2 times in the Array.
And let's say, we don't want duplicate entries. We want Mohan and Kriti to be present just once.
So, to remove duplicate items, we have used the uniq() method.
y = x.uniq()
That removes all the duplicate items and assigns the new values to a new array y
Now, if you look at the new array y. All the duplicate elements are removed from it.
But just remember, the array x is not changed. It has all the duplicates present in it.