What if you could clean your data with just one simple command?
Why Compact for removing nil values in Ruby? - Purpose & Use Cases
Imagine you have a list of items, but some are missing or empty (nil). You want to use only the real items, but you have to check each one by hand.
Manually checking each item is slow and tiring. You might forget some, make mistakes, or write long code that is hard to read.
The compact method quickly removes all the nil values from your list in one simple step, making your code clean and easy.
clean_list = [] list.each do |item| clean_list << item unless item.nil? end
clean_list = list.compact
You can focus on working with real data only, without worrying about empty or missing values slowing you down.
When collecting user inputs, some answers might be missing. Using compact helps you quickly get only the answers that were actually given.
Manually removing nil values is slow and error-prone.
compact removes all nils in one simple call.
This makes your code cleaner and easier to understand.
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
