Recall & Review
beginner
What does the
compact method do in Ruby?It returns a new array with all
nil values removed from the original array.Click to reveal answer
beginner
How do you use
compact on an array [1, nil, 2, nil, 3]?You call
[1, nil, 2, nil, 3].compact, which returns [1, 2, 3].Click to reveal answer
intermediate
What is the difference between
compact and compact!?compact returns a new array without nil values, while compact! modifies the original array in place and returns nil if no changes were made.Click to reveal answer
beginner
Can
compact remove values other than nil?No,
compact only removes nil values. Other falsy values like false remain in the array.Click to reveal answer
beginner
Why might you want to use
compact in your Ruby code?To clean up arrays by removing unwanted
nil values, which can help avoid errors or simplify processing.Click to reveal answer
What does
[1, nil, 3].compact return?✗ Incorrect
The
compact method removes all nil values, so the result is [1, 3].What does
compact! do differently than compact?✗ Incorrect
compact! changes the original array by removing nil values directly.If an array has no
nil values, what does compact! return?✗ Incorrect
compact! returns nil if no changes were made to the array.Which values does
compact remove?✗ Incorrect
compact removes only nil values, not false.How can
compact help in Ruby programming?✗ Incorrect
compact helps clean arrays by removing nil values, making code safer and simpler.Explain how the
compact method works in Ruby and when you might use it.Think about cleaning up arrays by removing empty spots.
You got /4 concepts.
Describe the difference between
compact and compact! in Ruby.One changes the original array, the other does not.
You got /3 concepts.