Bird
Raised Fist0
Rubyprogramming~10 mins

Select/filter for filtering in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Select/filter for filtering
Start with array
↓
Check each element
↓
Condition true?
No→Skip element
Yes↓
Add element to new array
↓
More elements?
Yes→Check next element
No↓
Return filtered array
The program checks each item in the array, keeps only those that meet the condition, and returns a new array with them.
Execution Sample
Ruby
numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |n| n.even? }
puts even_numbers
This code selects only even numbers from the list and prints them.
Execution Table
StepElement (n)Condition n.even?ActionFiltered Array
11falseSkip[]
22trueAdd 2[2]
33falseSkip[2]
44trueAdd 4[2, 4]
55falseSkip[2, 4]
6End-Return filtered array[2, 4]
💡 All elements checked, filtering complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
filtered_array[][][2][2][2, 4][2, 4][2, 4]
Key Moments - 2 Insights
Why is the number 1 not added to the filtered array?
Because at step 1 in the execution_table, the condition n.even? is false for 1, so it is skipped.
Does the original array change after select/filter?
No, the original array stays the same; select/filter creates a new array with only the matching elements, as shown by filtered_array starting empty and filling up.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the filtered array after adding element 4?
A[4]
B[2, 4]
C[2]
D[]
💡 Hint
Check the 'Filtered Array' column at step 4 in the execution_table.
At which step does the condition n.even? become false for the first time?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Condition n.even?' column in the execution_table from the start.
If we changed the condition to n > 3, what would be the filtered array after step 5?
A[5]
B[2, 4]
C[4, 5]
D[1, 2, 3]
💡 Hint
Think about which numbers in the original array are greater than 3.
Concept Snapshot
Use select or filter to keep only elements that match a condition.
Syntax: array.select { |item| condition }
Returns a new array with matching items.
Original array stays unchanged.
Common for filtering lists easily.
Full Transcript
This visual execution shows how Ruby's select method filters an array. Starting with the array [1, 2, 3, 4, 5], each element is checked if it is even. If true, the element is added to a new array. Steps show 1 is skipped, 2 added, 3 skipped, 4 added, 5 skipped. The final filtered array is [2, 4]. The original array remains unchanged. This helps beginners see how select works step-by-step.

Practice

(1/5)
1. What does the select method do in Ruby when used on an array?
easy
A. It returns a new array with elements that meet the condition in the block.
B. It changes the original array by removing elements that don't meet the condition.
C. It counts how many elements meet the condition in the block.
D. It sorts the array based on the block's return value.

Solution

  1. Step 1: Understand the purpose of select

    The select method filters elements from an array based on a condition given in a block.
  2. Step 2: Check the behavior of select

    It returns a new array containing only elements for which the block returns true, without changing the original array.
  3. Final Answer:

    It returns a new array with elements that meet the condition in the block. -> Option A
  4. Quick Check:

    select filters and returns new array [OK]
Hint: Remember: select returns a new filtered array, original stays same [OK]
Common Mistakes:
  • Thinking select modifies the original array
  • Confusing select with count or sort
  • Believing select returns a single element
2. Which of the following is the correct syntax to filter even numbers from an array nums using select?
easy
A. nums.select(n) { n % 2 == 0 }
B. nums.select do n n % 2 == 0 end
C. nums.select { n => n % 2 == 0 }
D. nums.select { |n| n % 2 == 0 }

Solution

  1. Step 1: Review Ruby block syntax for select

    The select method requires a block with a parameter and a condition, usually written as { |param| condition }.
  2. Step 2: Identify correct syntax

    nums.select { |n| n % 2 == 0 } uses correct block syntax with |n| and condition n % 2 == 0. Other options have syntax errors or wrong block usage.
  3. Final Answer:

    nums.select { |n| n % 2 == 0 } -> Option D
  4. Quick Check:

    Correct block syntax = nums.select { |n| n % 2 == 0 } [OK]
Hint: Use curly braces with |variable| for select blocks [OK]
Common Mistakes:
  • Missing vertical bars around block variable
  • Using wrong block delimiters or syntax
  • Passing argument inside select parentheses
3. What is the output of this Ruby code?
numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |num| num.even? }
puts even_numbers.inspect
medium
A. [1, 3, 5]
B. [1, 2, 3, 4, 5]
C. [2, 4]
D. []

Solution

  1. Step 1: Understand the select block condition

    The block uses num.even? which returns true for even numbers only.
  2. Step 2: Filter the array based on condition

    From [1, 2, 3, 4, 5], only 2 and 4 are even, so the new array is [2, 4].
  3. Final Answer:

    [2, 4] -> Option C
  4. Quick Check:

    select with even? filters evens = [2, 4] [OK]
Hint: Remember even? returns true only for even numbers [OK]
Common Mistakes:
  • Confusing even? with odd?
  • Expecting original array unchanged output
  • Forgetting select returns new filtered array
4. Find the error in this Ruby code that tries to filter strings longer than 3 characters:
words = ["cat", "dog", "elephant", "fox"]
long_words = words.select |word| word.length > 3
puts long_words.inspect
medium
A. Using select instead of filter
B. Missing curly braces or do..end around the block
C. Incorrect method name length instead of size
D. Using puts instead of print

Solution

  1. Step 1: Identify block syntax error

    The code uses select |word| word.length > 3 without curly braces or do..end, which is invalid syntax.
  2. Step 2: Correct block syntax

    It should be select { |word| word.length > 3 } or select do |word| word.length > 3 end.
  3. Final Answer:

    Missing curly braces or do..end around the block -> Option B
  4. Quick Check:

    Block syntax requires braces or do..end [OK]
Hint: Always wrap select block with {} or do..end [OK]
Common Mistakes:
  • Omitting block delimiters
  • Confusing length and size methods (both work but length is correct)
  • Thinking select and filter are different methods
5. Given a hash scores = { alice: 50, bob: 75, carol: 40, dave: 90 }, how do you select only the key-value pairs where the score is 50 or more?
hard
A. scores.select { |name, score| score >= 50 }
B. scores.filter { |score| score >= 50 }
C. scores.select { |name| name >= 50 }
D. scores.filter { |name, score| name >= 50 }

Solution

  1. Step 1: Understand hash select/filter usage

    When using select on a hash, the block receives two parameters: key and value.
  2. Step 2: Apply condition on value (score)

    We want pairs where score >= 50, so the block should check score >= 50.
  3. Step 3: Identify correct syntax

    scores.select { |name, score| score >= 50 } correctly uses |name, score| score >= 50. The other options have wrong parameters or conditions.
  4. Final Answer:

    scores.select { |name, score| score >= 50 } -> Option A
  5. Quick Check:

    Hash select with two block params and value condition [OK]
Hint: Use two block variables for hash select: |key, value| [OK]
Common Mistakes:
  • Using only one block variable for hash select
  • Checking condition on key instead of value
  • Confusing select and filter with wrong parameters
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