Home »
Ruby programming
Array.insert() Method with Example in Ruby
Ruby Array.insert() Method: Here, we are going to learn about the Array.insert() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 26, 2020
Array.insert() Method
In this article, we will study about Array.insert() method. You all must be thinking the method must be doing something related to the insertion of a certain element. 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 one of the examples of the Public instance method which is specially defined in the Ruby library for Array class. This method works in the way that it inserts the object before the element with the given index. If you provide the negative index then it will count the index from the backward of the Array instance. This method is one example of destructive methods where the changes made by these methods are permanent. There is no non-destructive version of this method.
Syntax:
Array_instance.insert(index,object)
Argument(s) required:
This method accepts two arguments. The first one is the index where you want to insert the object of any class and the second is the String having the name of the object. These two arguments are mandatory as their absence will make you see Exception. The number of arguments can be more as well but can never be less than 2.
Example 1:
=begin
Ruby program to demonstrate insert method
=end
# array declaration
Lang = ["C++","Java","Python","Ruby","Perl"]
puts "Array insert implementation."
# input the index
puts "Enter the index where you want to insert:"
ind = gets.chomp.to_i
# input the element to be inserted
puts "Enter the object which you want to insert:"
ele = gets.chomp
if(ind = Lang.insert(ind,ele))
puts "Object inserted properly"
else
puts "Error in inserting object"
end
puts "Array elements are:"
print Lang
Output
Array insert implementation.
Enter the index where you want to insert:
2
Enter the object which you want to insert:
HTML
Object inserted properly
Array elements are:
["C++", "Java", "HTML", "Python", "Ruby", "Perl"]
Explanation:
In the above code, you can observe that we have asked the user for the index where she wants to insert the object and the name of the object. In the last part of output where the whole Array object is printed, you can observe that we have "HTML" at that index only.
Example 2:
=begin
Ruby program to demonstrate insert method
=end
# array declaration
Lang = ["C++","Java","Python","Ruby","Perl"]
puts "Array insert implementation."
# input the index
puts "Enter the first index where you want to insert:"
ind1 = gets.chomp.to_i
# input the elements
puts "Enter the first object which you want to insert:"
ele1 = gets.chomp
puts "Enter the second object where you want to insert:"
ele2 = gets.chomp
puts "Enter the third object which you want to insert:"
ele3 = gets.chomp
if(ind = Lang.insert(ind1,ele1,ele2,ele3))
puts "Object inserted properly"
else
puts "Error in inserting object"
end
puts "Array elements are:"
print Lang
Output
Array insert implementation.
Enter the first index where you want to insert:
2
Enter the first object which you want to insert:
Java
Enter the second object where you want to insert:
JavaScript
Enter the third object which you want to insert:
COBOL
Object inserted properly
Array elements are:
["C++", "Java", "Java", "JavaScript", "COBOL", "Python", "Ruby", "Perl"]
Explanation:
In the above code you can see that we inserting a number of elements at the same time in the Array instance. We have asked user for the index from where she wants to start inserting the objects. The first object is inserted at that location and others are inserted at the consecutive locations.