Home » Ruby programming

Adding elements into an array instance with << operator in Ruby

Here, we are going to learn how to add elements into an array instance with << operator in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on January 05, 2020

In the previous articles, we have gone through ways through which we can create Array instances. Some of them were Public instance methods and some were Public class methods. We should also know how they both differ from each other. Now we know multiple ways through which we can declare or generate our Array instances. Some are direct bypassing some arguments and some with the help of previously defined Array objects. Now, we will learn how we can add some elements to the previously defined Array? In this article, we will be learning about << with the help of which we can add elements to the instances of Array class.

Method description:

This is a public instance method. As discussed above, this method is used to add elements in a previously declared object of the Array class. This method works in a way that pushes the object to the end of the Array instance which is passed as the parameter to this symbol. This is a destructive method by nature as the changes created by this method are permanent and can't be changed later.

Syntax:

    array_instance << object

Parameter(s):

This method takes only one parameter which is the instance of Array and it is passed at the left-hand side of the operator or method.

Example 1:

=begin
  Ruby program to add an Array to Another 
  with the help of <<
=end

# array declaration
old_arr1 = ['Payal','Samir','Sonakshi','Hira','Panna']

# adding elements
old_arr1 << 'Garvit'
old_arr1 << 'Monika'
old_arr1 << 'Anushree'

# printing the array
puts "The new String Array Instance is:"
print old_arr1

Output

The new String Array Instance is:
["Payal", "Samir", "Sonakshi", "Hira", "Panna", "Garvit", "Monika", "Anushree"]

Explanation:

In the above code, you can observe that we are pushing or adding a String class object at the end of the Array instance which is passed as the parameter to the << operator or method. At the last when we are printing the Array object then you can observe the reflection of that object in the Array instance.

Example 2:

=begin
  Ruby program to add an Array to Another 
  with the help of <<
=end

# array declarations
old_arr1 = ['Ramit','Amit','Suresh','Payal']
old_arr2 = ['Payal','Samir','Sonakshi','Hira','Panna']

# adding elements of old_arr2 to old_arr1
old_arr1 << old_arr2

# printing array elements
puts "The new String Array Instance is: "
print old_arr1

Output

The new String Array Instance is: 
["Ramit", "Amit", "Suresh", "Payal", ["Payal", "Samir", "Sonakshi", "Hira", "Panna"]]

Explanation:

In the above code, you can observe that we are adding or pushing an Array instance to the end of another Array. Now our second Array is residing in the first Array at the last index. So, it can be accessed with the help of the last index only.




Comments and Discussions!

Load comments ↻






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