In Ruby, operators are methods because everything is an object, and operators are just special method calls that make code more readable and flexible.
Click to reveal answer
beginner
How does Ruby treat the expression 5 + 3 internally?
Ruby treats 5 + 3 as 5.+(3), calling the + method on the number 5 with 3 as an argument.
Click to reveal answer
intermediate
What advantage does making operators methods give in Ruby?
It allows you to redefine or customize operators for your own classes, making your objects behave like built-in types with familiar syntax.
Click to reveal answer
intermediate
Can you override the + operator in your own Ruby class? Why?
Yes, because <code>+</code> is a method, you can define or override it in your class to specify how objects add together.
Click to reveal answer
beginner
What does it mean that Ruby is 'everything is an object' in relation to operators?
It means all data types are objects, and operators are just methods called on these objects, so operators follow the same rules as other methods.
Click to reveal answer
In Ruby, what does the expression a + b actually do?
ACreates a new operator object combining <code>a</code> and <code>b</code>
BCalls the <code>+</code> method on object <code>a</code> with <code>b</code> as argument
CCalls a global function named plus with <code>a</code> and <code>b</code>
DAdds <code>a</code> and <code>b</code> directly without method calls
✗ Incorrect
Ruby treats operators like + as methods called on the left object, so a + b is a.+(b).
Why can you redefine operators in Ruby classes?
ABecause Ruby has special operator files
BBecause Ruby does not allow operator redefinition
CBecause operators are global functions
DBecause operators are methods and methods can be defined or overridden
✗ Incorrect
Operators are just methods, so you can define or override them in your classes like any other method.
What does 'everything is an object' mean in Ruby?
AObjects are only used for classes
BOnly numbers and strings are objects
CAll data types are objects and have methods
DRuby does not use objects
✗ Incorrect
In Ruby, all data types like numbers, strings are objects with methods.
Which of these is a correct way to call the addition operator as a method in Ruby?
A5.+(3)
B5 + (3)
Cadd(5,3)
D5.add(3)
✗ Incorrect
The operator + is a method, so calling 5.+(3) is the method form of 5 + 3.
What benefit does operator methods provide when designing your own Ruby classes?
AThey let you use familiar symbols like + or - to work with your objects
BThey prevent you from using operators
CThey make your code slower
DThey hide methods from users
✗ Incorrect
Defining operator methods lets you use simple symbols to perform actions on your objects, making code easier to read.
Explain why Ruby treats operators as methods and how this fits with Ruby's design philosophy.
Think about how Ruby treats numbers and strings as objects.
You got /4 concepts.
Describe how you would override the '+' operator in a Ruby class and why you might want to do that.
Remember operators are just methods you can write yourself.
You got /4 concepts.
Practice
(1/5)
1. Why are operators like + and - considered methods in Ruby?
easy
A. Because operators cannot be overridden in Ruby.
B. Because Ruby does not support functions.
C. Because operators are only used for numbers in Ruby.
D. Because they are actually method calls on objects, allowing customization.
Solution
Step 1: Understand Ruby's object model
In Ruby, everything is an object, and operators like + are methods called on these objects.
Step 2: Recognize method call behavior
This means you can override these operator methods to customize behavior for your own classes.
Final Answer:
Because they are actually method calls on objects, allowing customization. -> Option D
Quick Check:
Operators = methods on objects [OK]
Hint: Remember: operators are just special method calls [OK]
Common Mistakes:
Thinking operators are separate from methods
Believing Ruby only uses operators for numbers
Assuming operators cannot be customized
2. Which of the following is the correct way to define the + operator method in a Ruby class?
easy
A. def +(other); ... end
B. def plus(other); ... end
C. def add(other); ... end
D. def operator_plus(other); ... end
Solution
Step 1: Recall operator method syntax
In Ruby, operators like + are defined by naming the method exactly as the operator symbol.
Step 2: Match method name to operator
So to define +, you write def +(other), not other names like plus or add.
Final Answer:
def +(other); ... end -> Option A
Quick Check:
Operator method name = operator symbol [OK]
Hint: 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
3. What will be the output of this Ruby code?
class Box
attr_reader :value
def initialize(value)
@value = value
end
def +(other)
Box.new(@value + other.value)
end
end
box1 = Box.new(5)
box2 = Box.new(10)
result = box1 + box2
puts result.value
medium
A. Box object
B. 15
C. 510
D. Error
Solution
Step 1: Understand the + method in Box
The + method adds the @value of two Box objects and returns a new Box with the sum.
Step 2: Calculate the sum and output
box1 has value 5, box2 has value 10, so result.value is 5 + 10 = 15, which is printed.
Final Answer:
15 -> Option B
Quick Check:
5 + 10 = 15 [OK]
Hint: Operator methods can return new objects with combined values [OK]
Common Mistakes:
Expecting string concatenation instead of addition
Printing the object instead of its value
Confusing method return with side effects
4. Identify the error in this Ruby class that tries to override the * operator:
class Multiplier
def *(other)
@value * other
end
end
m = Multiplier.new
puts m * 3
medium
A. Wrong method name for operator *
B. Operator * cannot be overridden
C. Missing initialize method to set @value
D. Cannot multiply by a number
Solution
Step 1: Check class initialization
The class does not define an initialize method, so @value is never set.
Step 2: Understand the error cause
When m * 3 runs, @value is nil, causing a NoMethodError for * on nil.
Final Answer:
Missing initialize method to set @value -> Option C
Quick Check:
Uninitialized @value causes error [OK]
Hint: Always initialize instance variables before using them [OK]
Common Mistakes:
Assuming operator * cannot be overridden
Using wrong method name for operator
Not initializing instance variables
5. You want to create a Ruby class Vector2D that supports adding two vectors using +. Which approach best uses the fact that operators are methods in Ruby?
hard
A. Define def +(other) to return a new Vector2D with summed x and y.
B. Define a separate method add_vectors(v1, v2) outside the class.
C. Use a global function to add vector components.
D. Override the + operator to print a message instead of adding.
Solution
Step 1: Use operator method inside the class
Since operators are methods, defining def +(other) inside Vector2D allows natural use of + to add vectors.
Step 2: Return a new Vector2D with summed components
The method should return a new Vector2D object with x and y added from both vectors, making code clean and intuitive.
Final Answer:
Define def +(other) to return a new Vector2D with summed x and y. -> Option A