How to initialize array in Ruby
Last Updated :
24 Oct, 2019
In this article, we will learn how to initialize the
array in Ruby. There are several ways to create an array. Let's see each of them one by one.
Using the new class method:
new method can be used to create the arrays with the help of dot operator. Using arguments we can provide the size to array and elements to array.
Without any argument -
ruby
# creating array using new method
# without passing any parameter
arr = Array.new()
# displaying the size of arrays
# using size method
puts arr.size
Output:
0
Passing size of array as parameter -
ruby
# creating array using new method
# passing one parameter i.e. the
# size of array
arr2 = Array.new(7)
# displaying the length of arrays
# using length method
puts arr2.length
Output:
7
Passing size of array and elements as parameter -
ruby
# creating array using new method
# passing two parameters i.e. the
# size of array & element of array
arr3 = Array.new(4, "GFG")
puts "#{arr3}"
Output:
["GFG", "GFG", "GFG", "GFG"]
Using literal constructor[] -
In Ruby,
[] is known as the literal constructor which can be used to create the arrays.
ruby
# Ruby program to demonstrate the
# creation of array using literal
# constructor[] and to find the size
# and length of array
# creating array of characters
arr = Array['a', 'b', 'c', 'd', 'e', 'f']
# displaying array elements
puts "#{arr}"
# displaying array size
puts "Size of arr is: #{arr.size}"
# displaying array length
puts "Length of arr is: #{arr.length}"
Output:
["a", "b", "c", "d", "e", "f"]
[1, 2, 3, 4, 5, 6, 7]
Size of arr is: 6
Length of arr is: 6
Using range -
ruby
arr1 = ('1'..'6').to_a
# displaying array elements
puts "#{arr1}"
arr2 = *'11'..'15'
puts "#{arr2}"
Output:
["1", "2", "3", "4", "5", "6"]
["11", "12", "13", "14", "15"]
Similar Reads
How to Parse Hash in Ruby? Parsing a hash in Ruby entails gaining access to its keys and values, iterating over them, and carrying out any necessary actions. The article focuses on discussing the ways to parse a hash in Ruby. Table of Content Iterating through a HashAccessing Hash ElementsSorting a HashIterating through a Has
2 min read
The Initialize Method in Ruby The initialize method is useful when we want to initialize some class variables at the time of object creation. The initialize method is part of the object-creation process in Ruby and it allows us to set the initial values for an object. Below are some points about Initialize : We can define defaul
2 min read
Ruby | Array to_ary() function Array#to_ary() : to_ary() is a Array class method which returns self array representation. Syntax: Array.to_ary() Parameter: Array Return: self array representation. Example #1 : Ruby # Ruby code for to_ary() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88,
1 min read
Ruby | Array to_a() function Array#to_a() : to_a() is a Array class method which returns self array. Syntax: Array.to_a() Parameter: Array Return: self array representation Example #1 : Ruby # Ruby code for to_a() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c
1 min read
Ruby | Array to_s() function Array#to_s() : to_s() is a Array class method which returns self array. Syntax: Array.to_s() Parameter: Array Return: self array Example #1 : Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50,
1 min read
How to print output in Ruby? An essential aspect of programming involves the ability to print output, This allows developers to not only communicate information with users but also debug code and present results. Within Ruby, a potent and adaptable language for programming, numerous methods exist for printing output directly in
4 min read