0
0
Rubyprogramming~10 mins

Self keyword behavior in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Self keyword behavior
Start
Inside method
self refers to current object
Access or modify object's data
Method ends
The self keyword always points to the current object inside a method, letting you access or change that object's data.
Execution Sample
Ruby
class Person
  def initialize(name)
    @name = name
  end
  def show_self
    self
  end
end
p = Person.new("Alice")
p.show_self
This code creates a Person object and returns self inside the show_self method, which is the object itself.
Execution Table
StepActionself valueInstance variable @nameOutput
1Create Person object with name 'Alice'Person object (not nil)nil (before initialize)None
2Call initialize with 'Alice'Person objectSet @name = 'Alice'None
3Call show_self methodPerson object@name = 'Alice'Returns self (Person object)
4Print show_self resultPerson object@name = 'Alice'#<Person:0x... @name="Alice">
💡 Method show_self returns self, which is the current Person object.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
selfnilPerson object (new)Person object (in initialize)Person object (in show_self)Person object (returned)
@namenilnil'Alice''Alice''Alice'
Key Moments - 3 Insights
Why does self inside show_self refer to the Person object?
Because inside instance methods, self always points to the current object the method is called on, as shown in execution_table step 3.
Is self the same as @name?
No, self is the whole object, while @name is just one instance variable inside that object, as shown in variable_tracker.
What happens if we call self outside any method?
Outside methods, self refers to the main object or context, not the Person instance. This example focuses on inside methods only.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does self refer to inside show_self?
Anil
BThe Person object
CThe @name variable
DThe class Person itself
💡 Hint
Check the 'self value' column at step 3 in the execution_table.
At which step is the instance variable @name set to 'Alice'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Instance variable @name' column in the execution_table.
If we added a method that changes @name using self, what would self refer to?
AThe current Person object
BThe @name variable
CThe class Person
Dnil
💡 Hint
Remember from key_moments and execution_table that self inside instance methods is the current object.
Concept Snapshot
self keyword in Ruby:
- Inside instance methods, self is the current object.
- Use self to access or modify the object's data.
- self is not the same as instance variables like @name.
- Outside methods, self refers to the main context.
- Helps clarify which object is being referenced.
Full Transcript
This visual execution shows how the self keyword works in Ruby inside instance methods. When we create a Person object and call show_self, self inside that method points to the Person object itself. The instance variable @name holds the name 'Alice'. The execution table tracks each step: object creation, setting @name, calling show_self, and returning self. The variable tracker shows how self and @name change over time. Key moments clarify that self is the whole object, not just a variable, and that self inside methods always refers to the current object. The quiz tests understanding of self's meaning at different steps. This helps beginners see how self behaves step-by-step in Ruby.