Sometimes, lists have empty spots called nil in Ruby. compact helps clean the list by removing these empty spots.
Compact for removing nil values in Ruby
array.compactThis method returns a new array without nil values.
It does not change the original array unless you use compact!.
nil values and returns [1, 2, 3].[1, nil, 2, nil, 3].compact
nil, so it returns an empty array [].[nil, nil, nil].compact
compact! changes the original array by removing nil values.array = [1, nil, 2] array.compact! puts array.inspect
This program shows how compact removes nil values from the list without changing the original.
numbers = [10, nil, 20, nil, 30] clean_numbers = numbers.compact puts "Original: #{numbers.inspect}" puts "Cleaned: #{clean_numbers.inspect}"
If you want to remove nil values and change the original array, use compact!.
If there are no nil values, compact! returns nil.
compact removes nil values from arrays.
It returns a new array and keeps the original unchanged.
Use compact! to change the original array directly.
Practice
What does the Ruby method compact do when called on an array?
Solution
Step 1: Understand the purpose of
Thecompactcompactmethod creates a new array by removing allnilvalues from the original array.Step 2: Compare with other options
Other options describe different array methods or behaviors, but onlycompactremovesnilvalues without modifying the original array.Final Answer:
It returns a new array without any nil values. -> Option AQuick Check:
compact removes nil values = B [OK]
- Confusing compact with uniq (removes duplicates)
- Thinking compact sorts the array
- Believing compact replaces nil with zero
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]
Solution
Step 1: Identify method that returns new array without nil
compactreturns a new array withoutnilvalues and does not modify the original array.Step 2: Understand difference with
compact!compact!modifies the original array, so it changesarr.delete(nil)removes nil but modifiesarr.remove_nilis not a Ruby method.Final Answer:
arr.compact -> Option DQuick Check:
compact returns new array, compact! modifies [OK]
- Using compact! when original array should stay unchanged
- Using delete(nil) which modifies the original array
- Assuming remove_nil is a valid Ruby method
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
Solution
Step 1: Understand what
compactreturnscompactreturns a new array withoutnilvalues, sonew_arris [5, 10].Step 2: Check if original array changes
The original arrayarrremains unchanged as [nil, 5, nil, 10].Final Answer:
[5, 10] [nil, 5, nil, 10] -> Option AQuick Check:
compact returns new array, original unchanged = A [OK]
- Assuming original array changes after compact
- Confusing compact with compact! which modifies original
- Misreading output order
Consider this Ruby code snippet:
arr = [1, nil, 2, nil, 3] arr.compact puts arr.inspect
Why does the output still include nil values?
Solution
Step 1: Understand
compactbehaviorcompactreturns a new array withoutnilvalues but does not change the original array.Step 2: Check why
Sincearrstill hasnilarr.compactwas called without assignment,arrremains unchanged and still containsnil.Final Answer:
Because compact returns a new array but does not modify arr. -> Option CQuick Check:
compact returns new array, original unchanged = D [OK]
- Thinking compact modifies original array
- Believing puts cannot print arrays properly
- Assuming arr was reassigned automatically
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?
Solution
Step 1: Understand what
compactdoescompactremoves onlynilvalues and keeps all other values including0,false, and empty strings.Step 2: Analyze other options
select { |v| v }removes all falsy values (nil,false,0is truthy in Ruby but empty string is truthy), so it removesfalse.reject!(&:nil?)modifies the original array.delete_if { |v| v.nil? }modifies original array.Step 3: Choose best option for new array without nil
compactis simplest and returns new array without modifying original.Final Answer:
clean_data = data.compact -> Option BQuick Check:
compact removes nil only, keeps false and empty string = C [OK]
- Using select which removes false values
- Using delete_if which modifies original array
- Using reject! which modifies the original array
Ready to prove it?
Pass the test and this pattern moves unknown
