0
0
Rubyprogramming~10 mins

Everything is an object mental model in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Everything is an object mental model
Start: Any value
Is value a number?
YesTreat as object
Call methods on number
Is value a string?
YesTreat as object
Call methods on string
Is value a boolean?
YesTreat as object
Call methods on boolean
Is value nil?
YesTreat as object
Call methods on nil
All values are objects
Use methods, properties on any value
In Ruby, every value is an object, so you can call methods on numbers, strings, booleans, and even nil.
Execution Sample
Ruby
num = 5
puts num.class
str = "hi"
puts str.upcase
puts true.to_s
This code shows that numbers, strings, and booleans are all objects with methods.
Execution Table
StepCode LineVariableValueActionOutput
1num = 5num5 (Integer object)Assign integer object 5 to num
2puts num.classnum.classIntegerCall class method on numInteger
3str = "hi"str"hi" (String object)Assign string object to str
4puts str.upcasestr.upcase"HI"Call upcase method on strHI
5puts true.to_strue.to_s"true"Call to_s method on boolean truetrue
6EndProgram ends
💡 All lines executed; program ends normally.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
numundefined5 (Integer object)5 (Integer object)5 (Integer object)
strundefinedundefined"hi" (String object)"hi" (String object)
Key Moments - 2 Insights
Why can we call methods like .class or .upcase on numbers and strings?
Because in Ruby, everything is an object, including numbers and strings, so they have methods you can call. See execution_table rows 2 and 4 where methods are called on num and str.
Is 'true' a special value or an object?
'true' is a boolean object in Ruby, so you can call methods on it like .to_s as shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of calling num.class at step 2?
AInteger
BFixnum
CNumber
DObject
💡 Hint
Check the Output column in execution_table row 2.
At which step do we convert the string to uppercase?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Code Line and Action columns in execution_table.
If we tried to call a method on nil, what would happen based on the mental model?
AError because nil is not an object
BIt works because nil is also an object
CIt returns nil silently
DIt converts nil to a string automatically
💡 Hint
Refer to concept_flow where nil is treated as an object.
Concept Snapshot
In Ruby, everything is an object.
Numbers, strings, booleans, and nil all have methods.
You can call methods on any value.
This makes Ruby very consistent and flexible.
Example: 5.class returns Integer.
Example: "hi".upcase returns "HI".
Full Transcript
This visual trace shows that in Ruby, every value is an object. We assign the number 5 to variable num, which is an Integer object. Calling num.class returns 'Integer'. We assign the string 'hi' to variable str, which is a String object. Calling str.upcase returns 'HI'. We also call to_s on the boolean true, which returns the string 'true'. This demonstrates that numbers, strings, booleans, and even nil are objects with methods you can call. The execution table tracks each step, showing variable values and outputs. The concept flow diagram shows the decision path that all values are treated as objects, allowing method calls on any value. This mental model helps beginners understand Ruby's consistent object-oriented nature.