0
0
Rubyprogramming~10 mins

Why arrays are fundamental in Ruby - Test Your Understanding

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 in Ruby.

Ruby
my_array = [1]
Drag options to blanks, or click blank then click option'
A{}
B[]
C()
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which create a hash, not an array.
Using parentheses () which are for method calls or grouping.
2fill in blank
medium

Complete the code to add the number 5 to the end of the array.

Ruby
numbers = [1, 2, 3]
numbers.[1](5)
puts numbers.inspect
Drag options to blanks, or click blank then click option'
Apush
Bpop
Cshift
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes the last element.
Using shift which removes the first element.
3fill in blank
hard

Fix the error in the code to access the first element of the array.

Ruby
fruits = ['apple', 'banana', 'cherry']
first = fruits[1]
puts first
Drag options to blanks, or click blank then click option'
A[0]
B(0)
C(1)
D{0}
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using curly braces which are for hashes.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers 1 to 5.

Ruby
squares = (1..5).[1] { |n| n [2] n }
puts squares.inspect
Drag options to blanks, or click blank then click option'
Amap
B*
C+
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using select which filters elements instead of transforming.
Using + which adds numbers instead of multiplying.
5fill in blank
hard

Fill all three blanks to select even numbers and double them.

Ruby
result = (1..10).[1] { |x| x [2] 2 == 0 }.[3] { |x| x * 2 }
puts result.inspect
Drag options to blanks, or click blank then click option'
Amap
B%
Cselect
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter which is not a Ruby array method.
Using + instead of % to check evenness.