Bird
Raised Fist0
Rubyprogramming~10 mins

Find/detect for first match in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Find/detect for first match
Start with collection
↓
Check each element
↓
Does element match condition?
No→Next element
Yes↓
Return this element
↓
End
The program checks each item in a list one by one until it finds the first item that meets the condition, then it stops and returns that item.
Execution Sample
Ruby
numbers = [3, 7, 10, 15]
result = numbers.find { |n| n > 8 }
puts result
This code finds the first number greater than 8 in the list and prints it.
Execution Table
StepCurrent Element (n)Condition (n > 8)Match?ActionResult
133 > 8 => falseNoCheck next elementnil
277 > 8 => falseNoCheck next elementnil
31010 > 8 => trueYesReturn 10 and stop10
4---Stop iteration10
💡 Iteration stops when element 10 matches condition n > 8.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
n-3710-
resultnilnilnil1010
Key Moments - 2 Insights
Why does the search stop after finding 10 and not continue to 15?
Because the find method returns the first element that matches the condition and stops immediately, as shown in execution_table step 3.
What happens if no element matches the condition?
The find method returns nil, meaning no matching element was found, as seen in steps 1 and 2 where no match occurs yet result stays nil.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 2?
A3
B7
Cnil
D10
💡 Hint
Check the 'Result' column in execution_table row for step 2.
At which step does the condition n > 8 become true for the first time?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Condition' and 'Match?' columns in execution_table.
If the array was [1, 2, 3], what would 'result' be after running the same code?
A1
Bnil
C3
DError
💡 Hint
Refer to key_moments explanation about no matching element returning nil.
Concept Snapshot
Use find (or detect) to get the first element matching a condition.
Syntax: collection.find { |item| condition }
Returns the first matching item or nil if none found.
Stops checking after first match.
Useful to quickly locate an element in a list.
Full Transcript
This example shows how Ruby's find method works. It looks at each element in the array one by one. For each element, it checks if the element is greater than 8. If not, it moves to the next element. When it finds the first element that is greater than 8, it returns that element and stops checking further. If no element matches, it returns nil. This helps to quickly find the first item that meets a condition without checking the whole list.

Practice

(1/5)
1.

What does the Ruby method find (also known as detect) do when called on an array?

easy
A. It removes the first element that matches the condition in the block.
B. It returns all elements that match the condition in the block.
C. It returns the first element that matches the condition in the block.
D. It sorts the array based on the block's condition.

Solution

  1. Step 1: Understand the purpose of find

    The find method searches through the array elements one by one.
  2. Step 2: Identify what find returns

    It returns the first element for which the block condition is true, then stops searching.
  3. Final Answer:

    It returns the first element that matches the condition in the block. -> Option C
  4. Quick Check:

    find = first matching element [OK]
Hint: Remember: find returns only the first match, not all matches [OK]
Common Mistakes:
  • Thinking find returns all matches
  • Confusing find with methods that modify the array
  • Assuming find sorts the array
2.

Which of the following is the correct syntax to find the first even number in an array nums using find?

nums = [1, 3, 4, 6]
easy
A. nums.find { |n| n.even? }
B. nums.find(n.even?)
C. nums.find => n.even?
D. nums.find |n| n.even?

Solution

  1. Step 1: Recall the block syntax for find

    The block must be passed with curly braces or do-end and use a block variable with pipes.
  2. Step 2: Check each option's syntax

    nums.find { |n| n.even? } uses correct block syntax: { |n| n.even? }. Others have syntax errors.
  3. Final Answer:

    nums.find { |n| n.even? } -> Option A
  4. Quick Check:

    Correct block syntax = nums.find { |n| n.even? } [OK]
Hint: Use curly braces and pipes for blocks with find [OK]
Common Mistakes:
  • Missing pipes around block variable
  • Passing condition without a block
  • Using wrong arrow or syntax for block
3.

What is the output of the following Ruby code?

words = ["apple", "banana", "cherry", "date"]
result = words.find { |w| w.length > 5 }
puts result
medium
A. "cherry"
B. nil
C. "apple"
D. "banana"

Solution

  1. Step 1: Check each word's length

    "apple" (5), "banana" (6), "cherry" (6), "date" (4).
  2. Step 2: Find first word with length > 5

    "banana" is the first word longer than 5 characters.
  3. Final Answer:

    "banana" -> Option D
  4. Quick Check:

    First word with length > 5 = "banana" [OK]
Hint: Check elements in order; find stops at first match [OK]
Common Mistakes:
  • Choosing the last matching word instead of first
  • Confusing length condition
  • Assuming find returns all matches
4.

Identify the error in this Ruby code that tries to find the first negative number:

numbers = [3, 5, -2, 7]
result = numbers.find do |n|
  n < 0
end
puts result
medium
A. There is no error; the code works correctly.
B. The variable n is not defined in the block.
C. The block syntax is incorrect; should use curly braces.
D. The method find does not accept a block.

Solution

  1. Step 1: Check block syntax

    The do...end block with variable n is valid syntax for find.
  2. Step 2: Confirm method usage

    find accepts a block and returns the first element matching n < 0.
  3. Final Answer:

    There is no error; the code works correctly. -> Option A
  4. Quick Check:

    Valid block syntax and method usage = There is no error; the code works correctly. [OK]
Hint: Remember: do-end blocks are valid for find [OK]
Common Mistakes:
  • Thinking only curly braces work for blocks
  • Assuming variable must be declared outside block
  • Believing find can't take blocks
5.

You have an array of hashes representing people:

people = [
  {name: "Alice", age: 18},
  {name: "Bob", age: 17},
  {name: "Carol", age: 22}
]

Which code finds the first person who is an adult (age 18 or older) and returns their name?

hard
A. people.detect { |p| p[:age] > 18 }[:name]
B. people.find { |p| p[:age] >= 18 }[:name]
C. people.find { |p| p[:age] > 18 }[:name]
D. people.detect { |p| p[:age] >= 18 }[:age]

Solution

  1. Step 1: Understand the condition for adult

    Adult means age 18 or older, so condition is p[:age] >= 18.
  2. Step 2: Find first matching person and get name

    Use find or detect with condition, then access [:name].
  3. Step 3: Check options carefully

    people.find { |p| p[:age] >= 18 }[:name] uses correct condition and returns name. people.detect { |p| p[:age] >= 18 }[:age] returns age, not name. Options A and C use > 18, excluding age 18.
  4. Final Answer:

    people.find { |p| p[:age] >= 18 }[:name] -> Option B
  5. Quick Check:

    Correct condition and return name = people.find { |p| p[:age] >= 18 }[:name] [OK]
Hint: Use >= 18 to include age 18, then get [:name] [OK]
Common Mistakes:
  • Using > 18 excludes age 18
  • Returning age instead of name
  • Confusing find and detect (both work but check return)
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