A predicate method is a method that ends with a question mark (?) and returns a boolean value (true or false). It answers a yes/no question about an object.
Click to reveal answer
beginner
Why do predicate methods in Ruby end with a question mark (?)?
The question mark at the end signals that the method returns a boolean value, making it clear the method answers a yes/no question.
Click to reveal answer
beginner
Example: What does the Ruby method `empty?` do when called on an array?
The `empty?` method returns true if the array has no elements, otherwise it returns false.
Click to reveal answer
intermediate
How would you define a custom predicate method in Ruby?
Define a method ending with `?` that returns true or false based on a condition. For example:
def adult?(age)
age >= 18
end
Click to reveal answer
intermediate
Can predicate methods return values other than true or false?
Technically yes, but by convention predicate methods should return only true or false to clearly indicate a yes/no answer.
Click to reveal answer
What does a predicate method in Ruby always return?
Atrue or false
Ba string
Can integer
Dnil
✗ Incorrect
Predicate methods always return true or false to answer yes/no questions.
Which of these is a valid predicate method name in Ruby?
Avalid?
Bcheck
Crun!
Dstart
✗ Incorrect
Predicate methods end with a question mark (?), like 'valid?'.
What does the Ruby method `nil?` check?
AIf an object is empty
BIf an object is a number
CIf an object is true
DIf an object is nil
✗ Incorrect
`nil?` returns true if the object is nil, otherwise false.
How do you call a predicate method named `empty?` on an array named `arr`?
Aarr.empty
Bempty?(arr)
Carr.empty?
Dempty.arr?
✗ Incorrect
Predicate methods are called like any other method: object.method_name?.
Why is it helpful to use predicate methods?
AThey speed up the program
BThey make code easier to read by clearly showing yes/no checks
CThey return strings
DThey create new objects
✗ Incorrect
Predicate methods improve readability by signaling boolean checks.
Explain what a predicate method is and why it ends with a question mark in Ruby.
Think about how Ruby shows methods that answer true or false questions.
You got /3 concepts.
Write a simple custom predicate method in Ruby that checks if a number is even.
Use the modulo operator (%) to check even numbers.
You got /3 concepts.
Practice
(1/5)
1. What does a predicate method in Ruby always return?
easy
A. A string value
B. A boolean value (true or false)
C. An integer value
D. An array
Solution
Step 1: Understand predicate method purpose
Predicate methods in Ruby are designed to answer yes/no questions about an object or data.
Step 2: Identify return type of predicate methods
They always return true or false, which are boolean values.
Hint: empty? returns true only for empty strings [OK]
Common Mistakes:
Assuming non-empty strings return true
Confusing empty? with nil? method
Ignoring the question mark in method names
4. Identify the error in this Ruby code:
def positive?(num)
if num > 0
true
else
false
end
end
puts positive?("5")
medium
A. ArgumentError because "5" is a string, not a number
B. SyntaxError due to missing end
C. No error, outputs true
D. Returns nil instead of true/false
Solution
Step 1: Analyze method input and comparison
The method compares num > 0, but "5" is a string, not a number.
Step 2: Understand Ruby type comparison
Comparing string "5" with integer 0 causes an ArgumentError at runtime.
Final Answer:
ArgumentError because "5" is a string, not a number -> Option A
Quick Check:
Comparing string and integer causes ArgumentError [OK]
Hint: Check input types before numeric comparisons [OK]
Common Mistakes:
Passing strings instead of numbers
Assuming no error on mixed type comparisons
Missing end keyword (not the case here)
5. You want to create a predicate method valid_name? that returns true only if a string is not empty and contains only letters. Which implementation is correct?
hard
A. def valid_name?(name)
name.nil? && name.match?(/[a-z]/)
end
B. def valid_name?(name)
name.empty? || name.match?(/\d+/)
end
C. def valid_name?(name)
name.length > 0 && name.include?(' ')
end
D. def valid_name?(name)
!name.empty? && name.match?(/^[a-zA-Z]+$/)
end
Solution
Step 1: Understand the requirements
The method must return true only if the string is not empty and contains only letters.
Step 2: Evaluate each option
def valid_name?(name)
!name.empty? && name.match?(/^[a-zA-Z]+$/)
end checks that name is not empty and matches only letters using regex /^[a-zA-Z]+$/. This meets the requirements.
Final Answer:
def valid_name?(name)
!name.empty? && name.match?(/^[a-zA-Z]+$/)
end -> Option D
Quick Check:
Not empty and only letters = valid_name? true [OK]
Hint: Use regex and empty? to validate strings in predicates [OK]
Common Mistakes:
Using || instead of && for conditions
Checking for digits instead of letters
Ignoring empty string check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.