Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the self keyword represent inside an instance method in Ruby?
easy
A. The superclass of the current class
B. The class itself
C. A global variable
D. The current instance (object) of the class

Solution

  1. Step 1: Understand instance methods context

    Inside an instance method, the code runs for a specific object created from the class.
  2. Step 2: Identify what self points to

    In this context, self refers to that specific object (instance) running the method.
  3. Final Answer:

    The current instance (object) of the class -> Option D
  4. Quick Check:

    self in instance method = instance [OK]
Hint: Inside instance methods, think "self" means the object itself [OK]
Common Mistakes:
  • Confusing self with the class inside instance methods
  • Thinking self is a global or class variable
  • Assuming self is always the class
2. Which of the following is the correct way to define a class method using self in Ruby?
easy
A. def self.my_method; 'hello'; end
B. def my_method.self; 'hello'; end
C. def my_method(self); 'hello'; end
D. def self:my_method; 'hello'; end

Solution

  1. Step 1: Recall class method syntax

    Class methods are defined by prefixing the method name with self. inside the class.
  2. Step 2: Check each option's syntax

    def self.my_method; 'hello'; end uses def self.my_method which is correct syntax. Others are invalid Ruby syntax.
  3. Final Answer:

    def self.my_method; 'hello'; end -> Option A
  4. Quick Check:

    Class method syntax = def self.method_name [OK]
Hint: Class methods start with def self.method_name [OK]
Common Mistakes:
  • Placing self after method name
  • Using colon instead of dot
  • Passing self as a parameter
3. What will be the output of the following Ruby code?
class Dog
  def speak
    self.class
  end
end

fido = Dog.new
puts fido.speak
medium
A. Dog
B. #
C. fido
D. Object

Solution

  1. Step 1: Understand what self is inside speak

    Inside the instance method speak, self is the instance fido.
  2. Step 2: Evaluate self.class

    Calling class on the instance returns its class, which is Dog.
  3. Final Answer:

    Dog -> Option A
  4. Quick Check:

    self.class in instance method = class name [OK]
Hint: self.class inside instance method returns the object's class [OK]
Common Mistakes:
  • Thinking self.class returns the instance
  • Confusing class with object name
  • Expecting output to be an object instead of class name
4. Identify the error in this Ruby code snippet:
class Cat
  def self.meow
    puts "Meow!"
  end

  def meow
    puts "Meow from instance"
  end

  def self.instance_meow
    meow
  end
end

Cat.instance_meow
medium
A. Missing self in instance method definition
B. Calling instance method meow inside a class method without an instance
C. Incorrect syntax for class method definition
D. Using puts inside methods is not allowed

Solution

  1. Step 1: Analyze method calls inside self.instance_meow

    This is a class method trying to call meow without specifying an instance.
  2. Step 2: Understand method types

    meow without self. is an instance method, so it needs an object to call it on.
  3. Step 3: Identify the error

    Calling an instance method directly inside a class method causes a NoMethodError because no instance is given.
  4. Final Answer:

    Calling instance method meow inside a class method without an instance -> Option B
  5. Quick Check:

    Class methods cannot call instance methods without an object [OK]
Hint: Class methods need an instance to call instance methods [OK]
Common Mistakes:
  • Trying to call instance methods directly inside class methods
  • Confusing self in class vs instance methods
  • Ignoring need for object to call instance methods
5. Given the following Ruby code, what will be the output?
class Calculator
  def self.description
    "I am a Calculator"
  end

  def initialize(value)
    @value = value
  end

  def show_description
    self.class.description
  end
end

calc = Calculator.new(10)
puts calc.show_description
hard
A. NoMethodError
B. "Calculator"
C. "I am a Calculator"
D. "10"

Solution

  1. Step 1: Understand self.class.description inside instance method

    Inside show_description, self is the instance calc. Calling class returns the class Calculator.
  2. Step 2: Call class method description

    Calling description on the class returns the string "I am a Calculator".
  3. Final Answer:

    "I am a Calculator" -> Option C
  4. Quick Check:

    Instance calls class method via self.class = output string [OK]
Hint: Use self.class to call class methods from instances [OK]
Common Mistakes:
  • Expecting instance variable value instead of class method output
  • Confusing self with class inside instance methods
  • Thinking NoMethodError will occur
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