Home » Ruby programming

Creating Array with Array.new(size){|index|} in Ruby

Creating Array in Ruby: In this tutorial, we are going to learn how to create an array with Array.new(size){|index|} in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on December 26, 2019

In the previous article, we have learned how we can declare an Array class instance with the help of the Array.new(Array) method? 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']

Now, after reading the previous articles, we have learnt to declare an Array class object in the following ways as well,

    array_name = Array.[](*args)
    array_name = Array.new(size = 0, obj = nil)
    array_name = Array.new(size){|index| block}

The above is the way we have used it in the last articles. We have learned three ways through which we can declare an Array in Ruby till time. Well, in this article, we will see that how we can declare an Array object with the help of Array.new(size){|index| block} method.

Method description:

This method is a public class method. Earlier Arrays are created by passing an index to them. In the case of this method, the value of each element is generated by passing the index of the element to the provided block and the returned value is stored.


    array_name = Array.new(size){|index| index#operation}


Arguments play a very important role in this method. This method will take the size as the Argument. Whole processing will be done with the help of the provided block.

Example 1:

    Ruby program to demonstrate the 
    Array.new(size){} method

# array declaration
arr = Array.new(5){|index| index*2}

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


Array elements are...


With the help of the above code, you can easily understand the implementation of the Array.new(size) method. We are creating an Array instance named "arr" with the help of this method. We want the Array object to be of size 5 and the elements should be of index multiple of 2. In the output, you can observe that the Array elements are 0, 2, 4, 6 and 8. The first element is 0 because we know that the indexing of an Array instance starts with 0 always. You can take input of the size from the user as well.

Example 2:

    Ruby program to demonstrate 
    the Array.new(size){} method

# array declaration
arr = Array.new(5){|index|}

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


Array elements are...


You can observe in the above code that when you are specifying the criteria through which you want to propagate the Array then nil values are assigned for size times. Here, five nil values are provided to the Array.

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.