0
0
Rubyprogramming~10 mins

Array creation methods in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an empty array.

Ruby
arr = [1]
Drag options to blanks, or click blank then click option'
A[]
B{}
C()
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} creates a hash, not an array.
2fill in blank
medium

Complete the code to create an array with numbers 1 to 5 using a range.

Ruby
arr = (1..[1]).to_a
Drag options to blanks, or click blank then click option'
A4
B6
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 4 excludes the number 5 from the array.
3fill in blank
hard

Fix the error in the code to create an array with 3 elements, all set to 0.

Ruby
arr = Array.new([1], 0)
Drag options to blanks, or click blank then click option'
A-1
B0
C5
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 creates an empty array, not an array with elements.
4fill in blank
hard

Fill both blanks to create an array of 4 elements, each initialized to its index multiplied by 2.

Ruby
arr = Array.new([1]) { |[2]| [2] * 2 }
Drag options to blanks, or click blank then click option'
A4
Bi
Cindex
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 for size creates 5 elements, not 4.
5fill in blank
hard

Fill all three blanks to create an array of 3 elements, each a string 'item' followed by its index.

Ruby
arr = Array.new([1]) { |[2]| "[3]#{ [2] }" }
Drag options to blanks, or click blank then click option'
A3
Bi
Citem
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' as string instead of variable name causes errors.