0
0
Rubyprogramming~10 mins

Predicate methods (ending with ?) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Predicate methods (ending with ?)
Call method ending with ?
Evaluate condition inside method
Return true or false
Use result in if/else or assignment
Predicate methods check a condition and return true or false, helping decisions in code.
Execution Sample
Ruby
def even?(num)
  num % 2 == 0
end

puts even?(4)
puts even?(5)
Defines a predicate method to check if a number is even, then prints true or false.
Execution Table
StepActionInputCondition EvaluatedReturn ValueOutput
1Call even?(4)44 % 2 == 0truetrue
2Print resulttrue--true
3Call even?(5)55 % 2 == 0falsefalse
4Print resultfalse--false
5End----
💡 All calls completed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
num-45-
return_value-truefalse-
Key Moments - 2 Insights
Why does the method name end with a question mark?
In Ruby, methods ending with ? are predicate methods that return true or false, as shown in steps 1 and 3 of the execution_table.
What does the method return when the condition is false?
The method returns false, as seen in step 3 where even?(5) returns false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value when calling even?(4)?
Atrue
Bfalse
C4
Dnil
💡 Hint
Check step 1 in the execution_table where even?(4) is called.
At which step does the method return false?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the return value column in the execution_table.
If we call even?(6), what would be the expected return value?
Afalse
Btrue
C6
Dnil
💡 Hint
Refer to the pattern in the execution_table for even numbers returning true.
Concept Snapshot
Predicate methods end with ? and return true or false.
They check a condition inside and return the result.
Used for clear yes/no questions in code.
Example: def even?(num); num % 2 == 0; end
Call with even?(4) returns true.
Full Transcript
Predicate methods in Ruby are special methods that end with a question mark. They always return true or false based on a condition. For example, the method even?(num) checks if a number is even by using the condition num % 2 == 0. When called with 4, it returns true, and with 5, it returns false. These methods help make code easier to read by clearly showing they answer yes/no questions. The execution table shows each step: calling the method, evaluating the condition, returning true or false, and printing the result.