Bird
Raised Fist0
Rubyprogramming~10 mins

Compact for removing nil values in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Compact for removing nil values
Start with array
↓
Check each element
↓
Is element nil?
Yes→Skip element
No↓
Keep element
↓
Build new array without nils
↓
Return new array
The method goes through each item in the array, skips nil values, and keeps all others to create a new array without nils.
Execution Sample
Ruby
arr = [1, nil, 2, nil, 3]
new_arr = arr.compact
puts new_arr.inspect
This code removes all nil values from the array and prints the new array.
Execution Table
StepCurrent ElementIs nil?ActionNew Array State
11NoKeep element[1]
2nilYesSkip element[1]
32NoKeep element[1, 2]
4nilYesSkip element[1, 2]
53NoKeep element[1, 2, 3]
6--Return new array[1, 2, 3]
💡 All elements checked; nil values removed; new array returned
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
arr[1, nil, 2, nil, 3][1, nil, 2, nil, 3][1, nil, 2, nil, 3][1, nil, 2, nil, 3][1, nil, 2, nil, 3][1, nil, 2, nil, 3][1, nil, 2, nil, 3]
new_arr[][1][1][1, 2][1, 2][1, 2, 3][1, 2, 3]
Key Moments - 2 Insights
Why does the new array not include nil values even though the original array has them?
Because at each step (see execution_table rows 2 and 4), the method checks if the element is nil and skips it, so nils are never added to the new array.
Does the original array change after using compact?
No, the original array stays the same (see variable_tracker for 'arr'), compact returns a new array without nils.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the new array state after step 3?
A[1, nil, 2]
B[1, 2]
C[1]
D[1, nil]
💡 Hint
Check the 'New Array State' column at step 3 in the execution_table.
At which step does the method skip a nil element for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Is nil?' and 'Action' columns in the execution_table.
If the original array had no nil values, how would the new array compare?
AIt would have only nil values
BIt would be empty
CIt would be the same as the original
DIt would have fewer elements
💡 Hint
Think about what compact does when no nils are present, check variable_tracker for 'new_arr'.
Concept Snapshot
Array#compact removes all nil values from an array.
It returns a new array without changing the original.
Each element is checked; nils are skipped.
Useful to clean arrays before processing.
Syntax: new_arr = old_arr.compact
Full Transcript
The Ruby method compact goes through each element of an array and removes all nil values. It creates a new array with only the non-nil elements. The original array stays unchanged. For example, given [1, nil, 2, nil, 3], compact returns [1, 2, 3]. This is useful when you want to clean your data by removing empty or missing values represented by nil. The execution table shows each step checking elements and building the new array. The variable tracker confirms the original array does not change, while the new array grows only with non-nil values.

Practice

(1/5)
1.

What does the Ruby method compact do when called on an array?

easy
A. It returns a new array without any nil values.
B. It removes all duplicate values from the array.
C. It sorts the array in ascending order.
D. It changes all nil values to zero.

Solution

  1. Step 1: Understand the purpose of compact

    The compact method creates a new array by removing all nil values from the original array.
  2. Step 2: Compare with other options

    Other options describe different array methods or behaviors, but only compact removes nil values without modifying the original array.
  3. Final Answer:

    It returns a new array without any nil values. -> Option A
  4. Quick Check:

    compact removes nil values = B [OK]
Hint: Remember: compact removes nil, keeps original unchanged [OK]
Common Mistakes:
  • Confusing compact with uniq (removes duplicates)
  • Thinking compact sorts the array
  • Believing compact replaces nil with zero
2.

Which of the following is the correct syntax to remove nil values from an array arr without changing arr itself?

arr = [1, nil, 2, nil, 3]
easy
A. arr.delete(nil)
B. arr.compact!
C. arr.remove_nil
D. arr.compact

Solution

  1. Step 1: Identify method that returns new array without nil

    compact returns a new array without nil values and does not modify the original array.
  2. Step 2: Understand difference with compact!

    compact! modifies the original array, so it changes arr. delete(nil) removes nil but modifies arr. remove_nil is not a Ruby method.
  3. Final Answer:

    arr.compact -> Option D
  4. Quick Check:

    compact returns new array, compact! modifies [OK]
Hint: Use compact for new array, compact! to modify original [OK]
Common Mistakes:
  • Using compact! when original array should stay unchanged
  • Using delete(nil) which modifies the original array
  • Assuming remove_nil is a valid Ruby method
3.

What is the output of the following Ruby code?

arr = [nil, 5, nil, 10]
new_arr = arr.compact
puts new_arr.inspect
puts arr.inspect
medium
A. [5, 10] [nil, 5, nil, 10]
B. [nil, 5, nil, 10] [5, 10]
C. [5, 10] [5, 10]
D. [nil, nil] [nil, 5, nil, 10]

Solution

  1. Step 1: Understand what compact returns

    compact returns a new array without nil values, so new_arr is [5, 10].
  2. Step 2: Check if original array changes

    The original array arr remains unchanged as [nil, 5, nil, 10].
  3. Final Answer:

    [5, 10] [nil, 5, nil, 10] -> Option A
  4. Quick Check:

    compact returns new array, original unchanged = A [OK]
Hint: compact returns new array; original stays same [OK]
Common Mistakes:
  • Assuming original array changes after compact
  • Confusing compact with compact! which modifies original
  • Misreading output order
4.

Consider this Ruby code snippet:

arr = [1, nil, 2, nil, 3]
arr.compact
puts arr.inspect

Why does the output still include nil values?

medium
A. Because arr was reassigned incorrectly.
B. Because compact does not remove nil values at all.
C. Because compact returns a new array but does not modify arr.
D. Because puts cannot print arrays correctly.

Solution

  1. Step 1: Understand compact behavior

    compact returns a new array without nil values but does not change the original array.
  2. Step 2: Check why arr still has nil

    Since arr.compact was called without assignment, arr remains unchanged and still contains nil.
  3. Final Answer:

    Because compact returns a new array but does not modify arr. -> Option C
  4. Quick Check:

    compact returns new array, original unchanged = D [OK]
Hint: Use compact! or assign compact result to change original [OK]
Common Mistakes:
  • Thinking compact modifies original array
  • Believing puts cannot print arrays properly
  • Assuming arr was reassigned automatically
5.

You have an array data = [nil, 0, false, '', nil, 5]. You want to remove only nil values but keep 0, false, and empty strings. Which code correctly does this?

hard
A. clean_data = data.reject!(&:nil?)
B. clean_data = data.compact
C. clean_data = data.select { |v| v }
D. clean_data = data.delete_if { |v| v.nil? }

Solution

  1. Step 1: Understand what compact does

    compact removes only nil values and keeps all other values including 0, false, and empty strings.
  2. Step 2: Analyze other options

    select { |v| v } removes all falsy values (nil, false, 0 is truthy in Ruby but empty string is truthy), so it removes false. reject!(&:nil?) modifies the original array. delete_if { |v| v.nil? } modifies original array.
  3. Step 3: Choose best option for new array without nil

    compact is simplest and returns new array without modifying original.
  4. Final Answer:

    clean_data = data.compact -> Option B
  5. Quick Check:

    compact removes nil only, keeps false and empty string = C [OK]
Hint: Use compact to remove nil only, keep false and empty strings [OK]
Common Mistakes:
  • Using select which removes false values
  • Using delete_if which modifies original array
  • Using reject! which modifies the original array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard