Bird
0
0

Which of the following is the correct way to define the + operator method in a Ruby class?

easy📝 Syntax Q12 of 15
Ruby - Operators and Expressions
Which of the following is the correct way to define the + operator method in a Ruby class?
Adef +(other); ... end
Bdef plus(other); ... end
Cdef add(other); ... end
Ddef operator_plus(other); ... end
Step-by-Step Solution
Solution:
  1. Step 1: Recall operator method syntax

    In Ruby, operators like + are defined by naming the method exactly as the operator symbol.
  2. Step 2: Match method name to operator

    So to define +, you write def +(other), not other names like plus or add.
  3. Final Answer:

    def +(other); ... end -> Option A
  4. Quick Check:

    Operator method name = operator symbol [OK]
Quick Trick: Operator methods use the operator symbol as the method name [OK]
Common Mistakes:
  • Using descriptive names instead of operator symbols
  • Trying to define operators with words like 'plus'
  • Confusing method names with operator symbols

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes