How to initialize array in Ruby Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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"] Comment More info S Shivam_k Follow Improve Article Tags : Ruby Ruby Array Explore Ruby Programming Language 4 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read Like