Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Everything is an object mental model
Start: Any value
↓
Is value a number?
Yes→Treat as object
↓
Call methods on number
↓
Is value a string?
Yes→Treat as object
↓
Call methods on string
↓
Is value a boolean?
Yes→Treat as object
↓
Call methods on boolean
↓
Is value nil?
Yes→Treat 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.

Practice

(1/5)
1. In Ruby, which of the following is not considered an object?
easy
A. Numbers like 5
B. Strings like "hello"
C. Booleans like true
D. Variables themselves

Solution

  1. Step 1: Understand Ruby's object model

    In Ruby, everything you work with directly, like numbers, strings, and booleans, are objects.
  2. Step 2: Differentiate variables from objects

    Variables are names or references pointing to objects, but they themselves are not objects.
  3. Final Answer:

    Variables themselves are not objects -> Option D
  4. Quick Check:

    Variables are references, not objects [OK]
Hint: Remember: variables point to objects, they aren't objects themselves [OK]
Common Mistakes:
  • Thinking variables are objects
  • Confusing object instances with variable names
  • Assuming booleans are not objects
2. Which of the following is the correct way to call the method upcase on a string object name in Ruby?
easy
A. name.upcase
B. upcase(name)
C. name->upcase()
D. upcase.name()

Solution

  1. Step 1: Recall Ruby method call syntax

    In Ruby, methods are called on objects using dot notation: object.method.
  2. Step 2: Apply to the string object

    For the string variable name, calling upcase is done as name.upcase.
  3. Final Answer:

    name.upcase -> Option A
  4. Quick Check:

    Method call syntax = object.method [OK]
Hint: Use dot (.) to call methods on objects in Ruby [OK]
Common Mistakes:
  • Using function-style calls like upcase(name)
  • Using arrow (->) notation from other languages
  • Placing method after dot incorrectly
3. What will be the output of the following Ruby code?
num = 10
puts num.class
medium
A. Integer
B. Fixnum
C. Numeric
D. Object

Solution

  1. Step 1: Understand the class method

    The class method returns the class of the object it is called on.
  2. Step 2: Identify the class of the number 10 in Ruby 3.12+

    In modern Ruby versions, integers are instances of the Integer class (Fixnum and Bignum were unified).
  3. Final Answer:

    Integer -> Option A
  4. Quick Check:

    10.class = Integer [OK]
Hint: Use .class to find an object's type in Ruby [OK]
Common Mistakes:
  • Choosing Fixnum (old Ruby versions)
  • Confusing Numeric with Integer
  • Thinking class returns Object always
4. The following Ruby code prints nothing. What is the problem?
text = "HELLO"
puts text.upcase!
medium
A. Missing parentheses for method call
B. Using upcase! on a string literal causes error
C. The method upcase! returns nil if no changes made
D. Strings are not objects, so methods can't be called

Solution

  1. Step 1: Understand upcase! behavior

    The upcase! method modifies the string in place and returns nil if no changes were made.
  2. Step 2: Analyze the code's output

    Since "HELLO" is uppercase, upcase! makes no changes and returns nil, so puts prints nothing.
  3. Step 3: Identify the error cause

    The return value nil is printed when no mutation occurs.
  4. Final Answer:

    upcase! returns nil if no changes made -> Option C
  5. Quick Check:

    Bang methods can return nil if no change [OK]
Hint: Bang (!) methods may return nil if no change happens [OK]
Common Mistakes:
  • Thinking bang methods always return a string
  • Assuming strings are not objects
  • Missing that upcase! modifies in place
5. Given the code below, what will be the output?
class NumberWrapper
  def initialize(value)
    @value = value
  end

  def to_s
    "Value is: #{@value.to_s}"
  end
end

num = NumberWrapper.new(42)
puts num.to_s
hard
A. 42
B. Value is: 42
C. Value is: @value
D. Error: undefined method to_s

Solution

  1. Step 1: Understand the class and method definitions

    The class NumberWrapper stores a value and defines a to_s method that returns a string including the value.
  2. Step 2: Analyze the to_s method call

    The method returns the string "Value is: " plus the string version of @value, which is 42.
  3. Step 3: Check the output of puts num.to_s

    It prints the string returned by to_s, which is "Value is: 42".
  4. Final Answer:

    Value is: 42 -> Option B
  5. Quick Check:

    Custom to_s returns formatted string [OK]
Hint: to_s returns string representation of any object [OK]
Common Mistakes:
  • Expecting just the number without text
  • Confusing instance variable with string literal
  • Thinking to_s is undefined
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard