Home » Ruby programming

Removing elements from Array using Array.delete() and Array.delete_at() in Ruby

Ruby Array.delete() and Array.delete_at() Methods: Here, we are going to learn how to remove elements from Array using Array.delete() and Array.delete_at() in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 16, 2019

Ruby Array.delete() and Array.delete_at() methods

In the last article, we have seen how we can remove the elements from an instance of Array class with the help of Array.pop() and Array.shift()?

Those were the two methods that do not take any arguments. For a quick revision, let me remind you that Array.pop() removes the element from the backside or from the end of the Array so there is no need to pass any argument as whichever the element is found by the method as the last one will be removed. The same goes for the Array.shift() method. It is totally opposite of the Array.pop() and removes the article from the front of the Array. Here also, you don't need to pass any argument because whichever element is found by the method as the first element will be removed. In the article, we will discuss two more such methods which will help us to remove the elements from the Array and they are namely Array.delete() and Array.delete_at(). We will learn how to implement them with the help of their syntaxes and their supporting examples.

Removing elements from Array using Array.delete_at() method

In Array.delete_at() method, we have to pass the index of the element we want to delete from the instance of Array class. The interpreter will not give an error if you will provide an index that is not available in the Array. Instead, it will give you the null if the provided index is not found.

Syntax:

    Array.delete_at(index)

Here, index represents the position of the element in the Array to be deleted.

Program:

=begin
Ruby program to remove elements from Array using 
Array.delete_at() method	
=end

# Array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python']

# input the index/position
puts "Enter the index of element you want to delete:"
ind = gets.chomp.to_i

# checking the index bound and removing
# the element
if(ind>=0 && ind<Adc.count)
  # removing the element
  Adc.delete_at(ind)
  # printing the array
  puts "Array elements after remove operation:"
  puts Adc
else
	puts "Index is not valid"
end

Output

Enter the index of element you want to delete:
 3
Array elements after remove operation:
Includehelp.com
Ruby
C++
Java
Python

Explanation:

In the above code, you can observe that we are taking input from the user and that input is nothing but the index of the element which the user wants to remove from the Array. We are proceeding with the help of the Array.delete_at() method which is removing the element.

Removing elements from Array using Array.delete() method

As the name suggests, Array.delete() will delete all the elements present in the array which are having the same as passed in the method. Alike Array.delete_at(), this method doesn't work with the help of index instead we need to pass the element name inside this method.

Syntax:

    Array.delete(element)

Here, element represents the element in the Array to be deleted. (Note: It will search the availability of that element and will delete all the same name element from the Array.)

Program:

=begin
Ruby program to remove elements from Array using 
Array.delete	
=end

# array declaration
Adc = ['Includehelp.com','Ruby','C++','C#','Java','Python','C++']

# input the element
puts "Enter the element you want to delete:"
element = gets.chomp

# removing the element
Adc.delete(element)

# printing the element
puts "Array elements after remove operation:"
puts Adc

Output

RUN 1:
Enter the element you want to delete:
 C++
Array elements after remove operation:
Includehelp.com
Ruby
C#
Java
Python

RUN 2:
Enter the element you want to delete:
 Perl
Array elements after remove operation:
Includehelp.com
Ruby
C++
C#
Java
Python
C++

Explanation:

In the above code and output, you can observe that the user has entered "C++" as the argument and all the elements named "C++" got deleted from the Array. In RUN 2, you can observe that the method imposes no effect if the element is not found in the Array.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.