Hello coders, In this post, you will learn how to solve HackerRank Ruby Array Initialization Solution. This problem is a part of the Ruby Tutorial series. One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

HackerRank Ruby Array Initialization Solution
Let’s get started with Ruby Array Initialization Solution
Problem Statement
One of the most commonly used data structures in Ruby is a Ruby Array, and below we see various methods of initializing a ruby array.
Your task is to initialize three different variables as explained below.
- Initialize an empty array with the variable name
array
Hint
array = Array.new
or
array = []
- Initialize an array with exactly one
nil
element in it with the variable namearray_1
Hint
array_1 = Array.new(1)
or
array_1 = [nil]
- Initialize an array with exactly two elements with value
10
in it using the variable namearray_2
.
Hint
array_2 = Array.new(2, 10)
or
array_2 = [10, 10]
HackerRank Ruby Array Initialization Solution
# Initialize 3 variables here as explained in the problem statement array = Array.new(0) array_1 = Array.new(1) array_2 = Array.new(2,10)
Note: This problem (Ruby Array – Initialization) is generated by HackerRank but the solution is provided by Chase2Learn. This tutorial is only for Educational and Learning purpose.