In this example we are going to create array with two arguments in Ruby Syntax array_name = Array.new(size = 0, obj = nil); Example # array declaration arr1 = Array.new(size = 5, obj = "Ruby") # printing array elements puts "Elements of \'arr1\' are:" puts arr1 # creating an empty array arr2 = Array.new() puts "Number of elements present in \'arr2\' are: #{arr2.count}" Output
Size: Size of array we are going to create
object: The element we are going to store in Array.
Elements of 'arr1' are:
Ruby
Ruby
Ruby
Ruby
Ruby
Number of elements present in 'arr2' are: 0
how to create an array with Array.[](*args) in Ruby ?
What are some of your favorite gems? What are their alternatives?
Ruby program to check whether the given number is prime or not
Ruby program to generate random numbers
How to shuffle an array in Ruby?
What are the various Ruby runtimes, and how are they different?
Ruby program to Calculate the factorial of given number
How to get index of array element in Ruby
What are #method_missing and #send? Why are they useful?
Ruby program to check whether the given number is Armstrong
How to add/remove elements to Array in Ruby?
Ruby program to add two integer numbers
Ruby program to print Fibonacci series
Ruby program to check whether the given number is palindrome
How to Sort Array in Ruby?
How to Get Input with Gets in Ruby
Creating Array with Array.new(size, obj) in Ruby
How to create two dimensional array in ruby?
Program to Print Triangle of Numbers in Ruby
How to Replace array elements in Ruby?
Ruby program to reverse a string
What's the difference between a lambda, a block and a proc?
Ruby program to print an array