0
0
Rubyprogramming~15 mins

Find/detect for first match in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Find the First Even Number in a List
📖 Scenario: Imagine you have a list of numbers from a game score sheet. You want to find the first even number to see when the player first scored an even point.
🎯 Goal: You will write a Ruby program that finds the first even number in a list using the find method.
📋 What You'll Learn
Create an array called scores with the exact numbers: 3, 7, 8, 10, 15
Create a variable called is_even that holds a lambda to check if a number is even
Use the find method with is_even to get the first even number from scores
Print the found even number using puts
💡 Why This Matters
🌍 Real World
Finding the first item that meets a condition is common in data filtering, like finding the first available appointment or the first product in stock.
💼 Career
Many programming jobs require filtering lists or arrays to find specific data quickly, making this skill very useful.
Progress0 / 4 steps
1
Create the list of scores
Create an array called scores with these exact numbers: 3, 7, 8, 10, 15
Ruby
Need a hint?

Use square brackets [] to create an array and separate numbers with commas.

2
Create a lambda to check even numbers
Create a variable called is_even that holds a lambda which returns true if a number is even
Ruby
Need a hint?

Use ->(num) { num.even? } to create a lambda that checks if num is even.

3
Find the first even number
Use the find method on scores with the is_even lambda to get the first even number and store it in first_even
Ruby
Need a hint?

Use scores.find(&is_even) to find the first number where is_even returns true.

4
Print the first even number
Print the variable first_even using puts to show the first even number found
Ruby
Need a hint?

Use puts first_even to print the value.