Home »
Ruby programming
Hash.insert() Method with Example in Ruby
Ruby Hash.insert() Method: Here, we are going to learn about the Hash.insert() Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on March 01, 2020
Hash.insert() Method
In this article, we will study about Hash.insert() Method. You all must be thinking the method must be doing something which is related to the insertion of 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 of the examples 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 word 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
lang = ["C++","Java","Python","Ruby","Perl"]
puts "Array insert implementation."
puts "Enter the index where you want to insert:"
ind = gets.chomp.to_i
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
lang = ["C++","Java","Python","Ruby","Perl"]
puts "Array insert implementation."
puts "Enter the first index where you want to insert:"
ind1 = gets.chomp.to_i
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 several elements at the same time in the Array instance. We have asked the 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.