Why everything is an object in Ruby - Performance Analysis
We want to understand how Ruby treats all values as objects and what that means for running code.
How does this design affect the steps a program takes as it runs?
Analyze the time complexity of calling a method on different Ruby objects.
def print_class(obj)
puts obj.class
end
print_class(42)
print_class("hello")
print_class([1, 2, 3])
print_class({a: 1, b: 2})
This code calls the class method on various objects to show that all values respond to methods.
Look at what repeats when calling methods on objects.
- Primary operation: Calling a method on an object (like
class). - How many times: Once per call, repeated for each object passed.
Each method call takes a small, fixed number of steps regardless of the object size.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 object | 1 method call |
| 10 objects | 10 method calls |
| 100 objects | 100 method calls |
Pattern observation: The work grows directly with how many objects you call methods on.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of objects you work with.
[X] Wrong: "Since everything is an object, method calls must be slow or complex."
[OK] Correct: Ruby optimizes method calls so they are fast, and treating all values as objects keeps code simple and consistent.
Knowing that everything is an object helps you understand Ruby's design and how method calls scale, which is useful when explaining your code's behavior.
"What if we changed the method call to one that processes the object's contents, like summing an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand Ruby's data model
Ruby treats all data types, including numbers and booleans, as objects.Step 2: Recognize the benefit of uniform behavior
This means you can call methods on any data type, making the language consistent and flexible.Final Answer:
Because all data types can use methods and behave uniformly -> Option CQuick Check:
Everything is an object [OK]
- Thinking only complex data types are objects
- Believing Ruby restricts variables
- Assuming numbers are not objects
Solution
Step 1: Recall Ruby method call syntax
In Ruby, methods are called on objects using dot notation: object.method.Step 2: Apply to a number
Since numbers are objects, calling5.to_sconverts the number 5 to a string.Final Answer:
5.to_s -> Option AQuick Check:
Method call syntax [OK]
- Using function-style calls like to_s(5)
- Trying arrow notation like 5->to_s()
- Omitting the dot between object and method
puts true.class
Solution
Step 1: Understand the
The.classmethod.classmethod returns the class of the object it is called on.Step 2: Identify the class of
In Ruby, the boolean valuetruetrueis an instance of the classTrueClass.Final Answer:
TrueClass -> Option BQuick Check:
true.class = TrueClass [OK]
- Assuming true is Boolean type
- Expecting output 'Object'
- Thinking code causes an error
number = 10 puts number.length
Solution
Step 1: Check the object type and method
The variablenumberholds an integer (10), which is an object of class Integer.Step 2: Verify if Integer has a
Integer objects do not have alengthmethodlengthmethod, so calling it causes a NoMethodError.Final Answer:
Numbers do not have a length method -> Option AQuick Check:
Integer has no length method [OK]
- Assuming all objects have length method
- Thinking variable is undefined
- Confusing syntax errors with method errors
Solution
Step 1: Convert integer to string using
Since numbers are objects, you can callto_sto_son them to get a string representation.Step 2: Call
Strings have alengthon the resulting stringlengthmethod that returns the number of characters.Final Answer:
number.to_s.length -> Option DQuick Check:
Chain methods: to_s then length [OK]
- Calling length on integer directly
- Using function syntax instead of method chaining
- Reversing method order
Ready to prove it?
Pass the test and this pattern moves unknown
