Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand predicate method purpose

    Predicate methods in Ruby are designed to answer yes/no questions about an object or data.
  2. Step 2: Identify return type of predicate methods

    They always return true or false, which are boolean values.
  3. Final Answer:

    A boolean value (true or false) -> Option B
  4. Quick Check:

    Predicate methods return true/false [OK]
Hint: Predicate methods always answer yes/no questions [OK]
Common Mistakes:
  • Thinking predicate methods return strings or numbers
  • Confusing predicate methods with regular methods
  • Ignoring the question mark at the end
2. Which of the following is the correct way to define a predicate method in Ruby?
easy
A. def valid?; true; end
B. def valid; true; end
C. def valid!; true; end
D. def valid: true; end

Solution

  1. Step 1: Recognize predicate method naming

    Predicate methods must end with a question mark (?).
  2. Step 2: Check method syntax

    def valid?; true; end defines a method named valid? correctly with def and end keywords.
  3. Final Answer:

    def valid?; true; end -> Option A
  4. Quick Check:

    Method name ends with ? and syntax is correct [OK]
Hint: Predicate methods end with ? and use def ... end [OK]
Common Mistakes:
  • Omitting the question mark in method name
  • Using invalid method name characters like ! or :
  • Incorrect method definition syntax
3. What will be the output of this Ruby code?
def empty_string?(str)
  str.empty?
end

puts empty_string?("")
puts empty_string?("hello")
medium
A. false\ntrue
B. true\ntrue
C. true\nfalse
D. false\nfalse

Solution

  1. Step 1: Understand the method behavior

    The method empty_string? calls str.empty? which returns true if str is an empty string.
  2. Step 2: Evaluate the calls

    empty_string?("") returns true because "" is empty; empty_string?("hello") returns false because "hello" is not empty.
  3. Final Answer:

    true\nfalse -> Option C
  4. Quick Check:

    Empty string check returns true, non-empty returns false [OK]
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

  1. Step 1: Analyze method input and comparison

    The method compares num > 0, but "5" is a string, not a number.
  2. Step 2: Understand Ruby type comparison

    Comparing string "5" with integer 0 causes an ArgumentError at runtime.
  3. Final Answer:

    ArgumentError because "5" is a string, not a number -> Option A
  4. 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

  1. Step 1: Understand the requirements

    The method must return true only if the string is not empty and contains only letters.
  2. 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.
  3. Final Answer:

    def valid_name?(name) !name.empty? && name.match?(/^[a-zA-Z]+$/) end -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard