0
0
Rubyprogramming~30 mins

Predicate methods (ending with ?) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Predicate Methods Ending with ? in Ruby
📖 Scenario: You are building a simple program to check properties of numbers in a list. You want to find out which numbers are even and which are odd using Ruby's predicate methods.
🎯 Goal: Create a Ruby program that uses predicate methods ending with ? to check if numbers are even or odd, and then prints the results.
📋 What You'll Learn
Create an array called numbers with the exact values: 2, 5, 8, 11, 14
Create a variable called threshold and set it to 10
Use a for loop with variable num to iterate over numbers and inside the loop use the predicate method even? to check if num is even
Inside the loop, also check if num is greater than threshold
Print the number and whether it is even and if it is greater than the threshold in the format: "2 is even and less than or equal to 10" or "11 is odd and greater than 10"
💡 Why This Matters
🌍 Real World
Checking conditions like even or odd numbers is common in programs that handle data filtering, validation, or decision making.
💼 Career
Understanding predicate methods helps you write clear and readable code, which is important for software development jobs that require condition checks and logic.
Progress0 / 4 steps
1
Create the numbers array
Create an array called numbers with these exact values: 2, 5, 8, 11, 14
Ruby
Need a hint?

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

2
Set the threshold variable
Create a variable called threshold and set it to 10
Ruby
Need a hint?

Use = to assign the value 10 to the variable threshold.

3
Use a for loop and predicate methods
Use a for loop with variable num to iterate over numbers. Inside the loop, use the predicate method even? on num to check if it is even. Also check if num is greater than threshold.
Ruby
Need a hint?

Use for num in numbers to loop. Use num.even? to check evenness and num > threshold to compare.

4
Print the results
Inside the for loop, print the number and whether it is even or odd, and if it is greater than the threshold or not. Use this exact format: "2 is even and less than or equal to 10" or "11 is odd and greater than 10"
Ruby
Need a hint?

Use puts with string interpolation and the ternary operator ? : to print the message.