0
0
Rubyprogramming~10 mins

Why everything is an object in Ruby - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why everything is an object in Ruby
Start: Ruby code runs
Create value (e.g., number, string)
Wrap value in an object
Call method on object
Return result
End
Ruby treats every value as an object, so when code runs, values are wrapped as objects allowing method calls on them.
Execution Sample
Ruby
num = 5
puts num.class
puts num.to_s
This code shows that the number 5 is an object of class Integer and calls a method to convert it to a string.
Execution Table
StepActionValueObject ClassMethod CalledOutput
1Assign 5 to num5Integer objectNoneNone
2Call num.class5Integer objectclassInteger
3Call num.to_s5Integer objectto_s5
4puts outputsIntegerClass objectputsInteger
5puts outputs5String objectputs5
💡 All values are objects, so methods like class and to_s can be called on them.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
numundefined5 (Integer object)5 (Integer object)5 (Integer object)5 (Integer object)
Key Moments - 2 Insights
Why can we call methods like .class or .to_s on a number?
Because in Ruby, numbers are objects of class Integer, so they have methods attached. See execution_table rows 2 and 3.
Is the value 5 just a number or an object?
It is an Integer object, not just a raw number. Ruby wraps all values as objects. See variable_tracker and execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of num.class?
AString
B5
CInteger
DObject
💡 Hint
Check execution_table row 2 under Output column.
At which step is the method to_s called on num?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table Method Called column.
If num was a string "5" instead of integer 5, what would num.class return?
AInteger
BString
CObject
DFixnum
💡 Hint
Recall that strings are objects of class String in Ruby.
Concept Snapshot
In Ruby, everything is an object.
All values like numbers, strings, true/false are objects.
Objects have classes and methods.
You can call methods on any value.
This makes Ruby very consistent and flexible.
Full Transcript
When Ruby runs code, every value you create is actually an object. For example, the number 5 is not just a number but an Integer object. This means it has methods like class and to_s you can call. In the example, num = 5 assigns an Integer object to num. Calling num.class returns Integer, showing its class. Calling num.to_s converts it to a string "5". This shows Ruby treats all values as objects, allowing method calls on them. This consistent object model is why Ruby is easy and powerful to use.