0
0
Rubyprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Compact for removing nil values
Start with array
Check each element
Is element nil?
YesSkip 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.