0
0
Rubyprogramming~10 mins

Initialize method as constructor in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Initialize method as constructor
Create new object
Call initialize method
Set initial values to object variables
Return new object with set values
When you create a new object, Ruby automatically calls the initialize method to set up initial values.
Execution Sample
Ruby
class Person
  def initialize(name)
    @name = name
  end
end

p = Person.new("Alice")
puts p.instance_variable_get(:@name)
This code creates a Person object with a name set by the initialize method and then prints the name.
Execution Table
StepActionEvaluationResult
1Call Person.new("Alice")Calls initialize with name = "Alice"New Person object created
2Inside initialize@name = "Alice"Instance variable @name set to "Alice"
3Return from initializeNo explicit returnPerson object with @name = "Alice" returned
4Call puts p.instance_variable_get(:@name)Retrieve @name valueOutputs: Alice
💡 Object created with @name set; program ends after printing name.
Variable Tracker
VariableStartAfter Step 2Final
@namenil"Alice""Alice"
Key Moments - 2 Insights
Why do we use @name instead of just name inside initialize?
Using @name creates an instance variable that belongs to the object, so it keeps the value after initialize ends (see Step 2 in execution_table).
Does initialize return the new object explicitly?
No, initialize does not return anything explicitly; Ruby returns the new object automatically after initialize finishes (see Step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of @name after Step 2?
A"Bob"
Bnil
C"Alice"
Dundefined
💡 Hint
Check the variable_tracker row for @name after Step 2.
At which step does the initialize method set the instance variable?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Action column in execution_table where @name is assigned.
If we change initialize to accept two parameters, how would the execution_table change?
AStep 2 would not assign any variables
BStep 1 would show two arguments passed
CStep 4 would print both variables automatically
DNo changes needed
💡 Hint
Focus on the method call and parameters in Step 1 of execution_table.
Concept Snapshot
Ruby's initialize method runs automatically when new creates an object.
It sets up instance variables like @name to store object data.
You don't call initialize directly; new does it for you.
Initialize does not return the object explicitly; Ruby does.
Use @variable to keep data inside the object.
Full Transcript
When you create a new object in Ruby using ClassName.new, Ruby automatically calls the initialize method inside that class. This method is like a setup step where you can assign starting values to the object's instance variables, which are variables that belong to the object itself. For example, if you have a Person class with an initialize method that takes a name, when you do Person.new("Alice"), Ruby calls initialize with "Alice" and sets @name to "Alice". The initialize method does not return anything explicitly, but Ruby returns the new object with the instance variables set. Later, you can access these variables inside the object. This process helps you create objects that start with the data you want.