Home »
Ruby programming
Array.shift Method with Example in Ruby
Ruby Array.shift Method: Here, we are going to learn about the Array.shift method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 09, 2020
Array.shift Method
In this article, we will study about Array.shift method. You all must be thinking the method must be doing something which is related to shifting of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
Method description:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in such a way that if no argument is provided then it removes the first element from the Array instance and returns that Array instance. If you are providing an integer argument 'n' to this method, then it will remove first 'n' elements from the Array instance and returns a new Array instance which will contain those 'n' elements which are removed from the Array instance. This method is one of the examples of destructive methods where changes created by the method are permanent and directly affect the original arrangement of objects in the Array object.
Syntax:
array_instance.shift -> object or nil
or
array_instance.shift(n)-> new_array
Argument(s) required:
This method takes one argument which decides the length of the Array instance which is returned by the method.
Example 1:
=begin
Ruby program to demonstrate shift method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array shift implementation"
pq =table.shift
puts "The element which is removed is #{pq}"
puts "Array instance:"
print table
Output
Array shift implementation
The element which is removed is 2
Array instance:
[4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
In the above code, you can observe that we are shifting or removing the elements from the Array instance with the help of Array.shift method. You can observe that the Array instance is no more having an object which was residing on the 0th index because we have invoked Array.shift method along with the Array instance.
Example 2:
=begin
Ruby program to demonstrate shift method
=end
# array declaration
table = [2,4,6,8,10,12,14,16,18,20]
puts "Array shift implementation"
puts "Enter the number of objects you want to shift:"
num = gets.chomp.to_i
rn = table.shift(num)
puts "The objects shifted from Array instance is #{rn}"
puts "Array instance: #{table}"
Output
Array shift implementation
Enter the number of objects you want to shift:
3
The objects shifted from Array instance is [2, 4, 6]
Array instance: [8, 10, 12, 14, 16, 18, 20]
Explanation:
In the above code, you can observe that we are shifting or removing the elements from the Array instance with the help of Array.shift method. You can observe that the Array instance is no more having objects which were residing on the 0th, 1st, and 2nd indices because we have invoked Array.shift method along with the Array instance. The method is returning a new Array instance.