You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Ruby Array - Initialization Solution

Hackerrank Ruby Array - Initialization Solution

Beeze Aal
Beeze Aal

.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue}

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 name array_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 name array_2.

Hint

array_2 = Array.new(2, 10)

or

array_2 = [10, 10]

Solution in ruby

Approach 1.

array = []
array_1 = Array.new(1)
array_2 = Array.new(2) { 10 }

Approach 2.

array = Array.new
array_1 = Array.new(1)
array_2 = Array.new(2, 10)

Approach 3.

# Initialize 3 variables here as explained in the problem statement
array = []
array_1 = [nil]
array_2 = [10, 10]