Bird
Raised Fist0
Rubyprogramming~10 mins

Comparison operators in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Comparison operators
Start
↓
Evaluate Left Value
↓
Evaluate Right Value
↓
Apply Comparison Operator
↓
Result: true or false
↓
Use Result in Condition or Output
↓
End
The program compares two values using a comparison operator and produces true or false as the result.
Execution Sample
Ruby
a = 5
b = 3
result = a > b
puts result
This code compares if a is greater than b and prints the result.
Execution Table
StepActionEvaluationResult
1Assign a = 5a = 55
2Assign b = 3b = 33
3Compare a > b5 > 3true
4Assign result = trueresult = truetrue
5Print resultputs resulttrue
💡 Program ends after printing the comparison result.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
anil55555
bnilnil3333
resultnilnilnilniltruetrue
Key Moments - 2 Insights
Why does the comparison a > b return true instead of a number?
Because comparison operators in Ruby return a boolean value (true or false), not a numeric result. See step 3 in the execution_table where 5 > 3 evaluates to true.
What happens if we compare two equal values with == operator?
The comparison returns true if both values are equal, otherwise false. This is similar to step 3 but with == instead of >.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 4?
A5
Btrue
Cfalse
Dnil
💡 Hint
Check the 'result' variable value in variable_tracker after step 4.
At which step does the comparison a > b happen?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for the comparison operation.
If we change b to 7, what would be the result of a > b at step 3?
Atrue
Bnil
Cfalse
Derror
💡 Hint
Compare 5 > 7 logically to determine the boolean result.
Concept Snapshot
Comparison operators compare two values and return true or false.
Common operators: ==, !=, >, <, >=, <=.
Used in conditions and expressions.
Example: a > b returns true if a is greater than b.
They do not return numbers, only boolean values.
Full Transcript
This visual execution trace shows how comparison operators work in Ruby. We start by assigning values to variables a and b. Then we compare a > b, which evaluates to true or false. This boolean result is stored in the variable result and printed. The trace shows each step, variable changes, and the final output. Comparison operators always return true or false, not numbers. This helps in making decisions in code.

Practice

(1/5)
1. Which of the following comparison operators in Ruby checks if two values are equal?
easy
A. ==
B. =
C. !=
D. >

Solution

  1. Step 1: Understand the purpose of each operator

    == checks equality, = assigns values, != checks inequality, and > checks if left is greater than right.
  2. Step 2: Identify the equality operator

    The operator that compares two values for equality is ==.
  3. Final Answer:

    == -> Option A
  4. Quick Check:

    Equality operator = == [OK]
Hint: Equality uses double equals (==), not single equals (=) [OK]
Common Mistakes:
  • Confusing assignment (=) with equality (==)
  • Using != instead of ==
  • Thinking > checks equality
2. Which of the following is the correct syntax to check if variable a is not equal to variable b in Ruby?
easy
A. a !== b
B. a =! b
C. a <> b
D. a != b

Solution

  1. Step 1: Recall Ruby's not equal operator

    Ruby uses != to check if two values are not equal.
  2. Step 2: Check each option's syntax

    a != b is correct. a =! b is invalid syntax. a <> b is not Ruby syntax. a !== b is JavaScript syntax, not Ruby.
  3. Final Answer:

    a != b -> Option D
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Not equal in Ruby is !=, not =! or !== [OK]
Common Mistakes:
  • Using =! instead of !=
  • Using !== which is JavaScript, not Ruby
  • Using <> which is invalid in Ruby
3. What will be the output of the following Ruby code?
a = 5
b = 10
puts a < b
medium
A. true
B. false
C. 5
D. 10

Solution

  1. Step 1: Understand the comparison

    The code compares if a (5) is less than b (10).
  2. Step 2: Evaluate the comparison

    Since 5 is less than 10, the expression a < b is true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    5 < 10 = true [OK]
Hint: Less than is <, returns true or false [OK]
Common Mistakes:
  • Confusing output with variable values
  • Expecting numeric output instead of boolean
  • Mixing up < and > operators
4. Identify the error in the following Ruby code:
if x = 10
puts "x is 10"
end
medium
A. Variable x is not declared
B. Missing end keyword
C. Should use == to compare, not =
D. puts syntax is wrong

Solution

  1. Step 1: Analyze the if condition

    The code uses = which assigns 10 to x, not compares.
  2. Step 2: Correct the comparison operator

    To check if x equals 10, use == instead of =.
  3. Final Answer:

    Should use == to compare, not = -> Option C
  4. Quick Check:

    Comparison needs ==, = is assignment [OK]
Hint: Use == for comparison, = is for assignment [OK]
Common Mistakes:
  • Using = instead of ==
  • Thinking missing end keyword causes error
  • Assuming variable must be declared before use
5. You want to check if a number n is between 10 and 20 inclusive in Ruby. Which expression correctly uses comparison operators to do this?
hard
A. n > 10 || n < 20
B. n >= 10 && n <= 20
C. 10 <= n <= 20
D. n == 10 && n == 20

Solution

  1. Step 1: Understand the range check

    We want n to be at least 10 and at most 20, so both conditions must be true.
  2. Step 2: Evaluate each option

    n >= 10 && n <= 20 uses n >= 10 && n <= 20, which correctly checks both limits.
    10 <= n <= 20 uses chained comparison which is invalid in Ruby.
    n > 10 || n < 20 uses OR (||), which allows values outside the range.
    n == 10 && n == 20 checks if n equals both 10 and 20 simultaneously, which is impossible.
  3. Final Answer:

    n >= 10 && n <= 20 -> Option B
  4. Quick Check:

    Use && to combine two comparisons [OK]
Hint: Use && to combine two comparisons for range checks [OK]
Common Mistakes:
  • Trying chained comparisons like 10 <= n <= 20
  • Using || instead of && for range
  • Checking equality to both ends simultaneously
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