Home » Ruby programming

Creating Array with Array.[](*args) in Ruby

Creating Array in Ruby: In this tutorial, we are going to learn how to create an array with Array.[](*args) in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 26, 2019

In the previous articles, we have learned some of the methods available in the Ruby library specially defined and declared for instances of Array class. You can also notice that in the program codes written to demonstrate all those methods are having Array instances declared with the conventional method such as,

    array_name = ['ele1', 'ele2', 'ele3', ..., 'eleN']

The above is the way we have used so far in our articles to declare an array. In the upcoming articles, you will learn the different ways through which we can declare an array instance. Well, in this article, we will see how we can declare an Array object with the help of [](*args)?

Method description:

This method is a public class method. You will have to provide the elements inside the round brackets and it will return an Array instance populated with the elements provided by you at the time of declaration. This method is one of the easiest and important methods for Array declaration.

Syntax:

    array_name = Array.[]('ele1', 'ele2' , ... ,'eleN')

Parameter(s):

Arguments play a very important role in this method. If you don't provide any arguments, it will result in an empty Array with no elements in it. This method can take any number of arguments.

Example 1:

=begin
    Ruby program to demonstrate Array.[](*args)
=end

# array declaration
arr = Array.[](1,"Sameer","Graphic Era")

# printing array elements
puts "Elements of \'arr\' are:"
puts arr

# empty array declaration
arr1 = Array.[]()
puts "Number of elements present in \'arr1\' are: #{arr1.count}"

Output

Elements of 'arr' are:
1
Sameer
Graphic Era
Number of elements present in 'arr1' are: 0

Explanation:

With the help of the above code, you can easily understand the implementation of Array.[] method. You can also observe that if we are not giving parameters to the method, then the Array results into an empty Array and we have shown that with the help of Array.count method.

Example 2:

=begin
    Ruby program to demonstrate Array.[](*args)
=end

# input array elements
puts "Enter the first element you want to keep in the Array instance"
ele1 = gets.chomp
puts "Enter the second element you want to keep in the Array instance"
ele2 = gets.chomp
puts "Enter the third element you want to keep in the Array instance"
ele3 = gets.chomp
puts "Enter the fourth element you want to keep in the Array instance"
ele4 = gets.chomp

# creating array using input elements
arr = Array.[](ele1,ele2,ele3,ele4)

# printing array elements
puts "Array elements are:"
puts arr

Output

Enter the first element you want to keep in the Array instance
Hrithik 
Enter the second element you want to keep in the Array instance 
B.Tech
Enter the third element you want to keep in the Array instance
Graphic Era University
Enter the fourth element you want to keep in the Array instance 
India
Array elements are: 
Hrithik 
B.Tech
Graphic Era University
India

Explanation:

In the above code, you can observe that we are taking multiple inputs from the user and we are passing those inputs as the elements which will be stored in the Array instance.



Comments and Discussions!

Load comments ↻





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