Home » Ruby programming

Creating Array Instance with * Operator(new_array -> arr * int/string) in Ruby

Creating Array in Ruby: In this tutorial, we are going to learn how to create an array Instance with * Operator(new_array -> arr * int/string) in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 27, 2019

In the last articles, we have gone through the method by which we can create Array instance with the help of & operator. You all must know that in my articles that were related to Array creation are Public class methods and now in the upcoming articles, we will be learning about Public instance methods.

Now, you all must be thinking about what is the difference between Public class methods and Public instance methods, then to clear your doubt, Public class methods can be invoked as soon as Ruby processes the execution of the class whereas Public instance methods can only be executed after the creation of an Instance of that particular class. We have studied methods like Array.new() which is a resident of Public class methods but now will be studying Public instance methods. Well, in this article, we will be studying about creating the Array instance with the help of * operator.

Method description:

The * symbol is the way to create an Array object and it requires one previously defined Array instance. It is also known as Set repetition. This can be an alias for Array.join(Arr1). It works in the way that it returns a new Array instance having elements after joining the elements of the Array with the integer or string passed as the argument in the method. The order is maintained from the original Array.

Syntax:

    new_array = old_array * int/string

Parameter(s):

The '*' operator takes two arguments. The first one is the previously defined Array and the second one is String or integer.

Example 1:

=begin
  Ruby program to create Array with * operator 
=end

# arrays
old_arr1 = [23,44,66]

# creating a new array 
new_arr = old_arr1 * 4

# printing the array
puts "The new Integer Array Instance is:"
print new_arr

Output

The new Integer Array Instance is:
[23, 44, 66, 23, 44, 66, 23, 44, 66, 23, 44, 66]

Explanation:

In the above code, you can observe that we are creating an Array instance with the help of * operator. In resulting Array, you can see that our elements are repeated 4 times along with an additional four with the Array elements and we are providing that 4 as the argument to the method.

Example 2:

=begin
  Ruby program to create Array with * operator 
=end

# array
old_arr = ['Ramit','Amit','Suresh','Payal','Samir','Sonakshi','Hira','Panna']

# creating a new array
new_arr = old_arr * ","

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

Output

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

Explanation:

In the above code, you can observe that we are creating a String Array with the help of * operator. The character which we are providing is "," and it is getting integrated after every element of the Array.




Comments and Discussions!

Load comments ↻






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