Home » Ruby programming

Deleting elements from Array using Array.pop and Array.shift methods in Ruby

Ruby Array.pop and Array.shift methods: Here, we are going to learn how to delete an element from an array using Array.pop and Array.shift method in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 14, 2019

Ruby Array.pop and Array.shift methods

If you are reading an article that is related to deleting elements from the instance of Array class then it is expected from you that you are aware of the basic things related to Array such as how to declare it and how to find the size of the Array. If you don't know this basic information then you can read related articles and gather the information which is available at includehelp.com/ruby/. In this article, you will come to know about various methods that are available in Ruby's library specifically defined to delete the elements from the Array. We are very well aware of the fact that Ruby's arrays are dynamic in nature which means that we can store n number of elements in them without taking care or bothering about their predefined size. Ruby facilitates you with different methods through which you can remove or delete elements from the object of the Array class. Well, this article we will be discussing two of them which are namely Array.pop and Array.shift.

1) Deleting elements by using Array.pop method

Array.pop method will delete elements from the right-hand side of the Array or you can say that it removes elements from the end of the backside. If you are using Array.pop one time it means that you want to remove the last element of the Array.

Program:

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

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

# printing array elements 
puts "Array elements before removing..."
puts Adc

# calling Array.pop method to remove element
Adc.pop

# printing array elements 
puts "Array elements after removing..."
puts Adc

Output

Array elements before removing...
Includehelp.com
Ruby
C++
C#
Java
Python
Array elements after removing...
Includehelp.com
Ruby
C++
C#
Java

Explanation:

In the above code and its output, you can observe that Array.pop is removing the elements from the end of the Array. The element "python" is removed from the Array.

2) Deleting elements by using Array.shift method

It is just the opposite of Array.pop. Array.shift method will delete elements from the left-hand side of the Array or you can say that it removes elements from the beginning of the front side. If you are using Array.shift one time then it means that you want to remove the first element of the Array.

Program:

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

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

# printing array elements 
puts "Array elements before removing..."
puts Adc

# calling Array.shift method to remove element
Adc.shift

# printing array elements 
puts "Array elements after removing..."
puts Adc

Output

Array elements before removing...
Includehelp.com
Ruby
C++
C#
Java
Python
Array elements after removing...
Ruby
C++
C#
Java
Python

Explanation:

In the above code and its output, you can observe that Array.shift is removing the elements from the beginning of the Array. The element "Includehelp.com" is removed from the Array.



Comments and Discussions!

Load comments ↻





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