0
0
Rubyprogramming~5 mins

Array methods (length, include?, flatten) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the length method do when called on an array in Ruby?
It returns the number of elements in the array. Think of it like counting how many items are in a basket.
Click to reveal answer
beginner
How does the include? method work on an array?
It checks if a specific item is inside the array and returns true if found, otherwise false. Like asking, "Is this toy in my box?"
Click to reveal answer
intermediate
What is the purpose of the flatten method on an array?
It takes an array that has other arrays inside it and turns it into one simple list without nested arrays. Like spreading out a stack of papers into one flat pile.
Click to reveal answer
beginner
What will [1, 2, 3].include?(2) return?
It will return true because the number 2 is inside the array.
Click to reveal answer
intermediate
What is the result of [1, [2, 3], 4].flatten?
The result is [1, 2, 3, 4]. The nested array [2, 3] is flattened into the main array.
Click to reveal answer
What does array.length return?
AThe first element of the array
BThe last element of the array
CThe number of elements in the array
DTrue if array is empty
What will ["apple", "banana"].include?("banana") return?
Atrue
Bfalse
Cnil
Derror
What does flatten do to an array?
AReverses the array
BSorts the array
CRemoves duplicate elements
DTurns nested arrays into a single flat array
What is the output of [1, 2, 3].length?
A3
B2
C1
D0
What will [1, [2, [3, 4]]].flatten return?
A[1, 2, [3, 4]]
B[1, 2, 3, 4]
C[[1, 2], 3, 4]
D[1, [2, 3, 4]]
Explain how you would check if an array contains a specific item in Ruby.
Think about asking if your list has a certain thing inside.
You got /3 concepts.
    Describe what happens when you use the flatten method on an array with nested arrays.
    Imagine spreading out a stack of papers into one flat pile.
    You got /3 concepts.