📖 Scenario: Imagine you are creating a simple Ruby class to represent a person. You want to understand how the self keyword works inside instance methods and class methods.
🎯 Goal: Build a Ruby class called Person that uses the self keyword to show how it refers to the current object inside instance methods and to the class itself inside class methods.
📋 What You'll Learn
Create a class called Person with an instance variable @name.
Add an instance method called show_self_instance that returns self.
Add a class method called show_self_class that returns self.
Create an instance of Person with the name "Alice".
Call both methods and print their results.
💡 Why This Matters
🌍 Real World
Understanding <code>self</code> helps you write clear Ruby classes that behave correctly when methods are called on objects or the class itself.
💼 Career
Many Ruby jobs require knowledge of object-oriented programming and how <code>self</code> works to build maintainable and bug-free code.
Progress0 / 4 steps
1
Create the Person class with an initialize method
Create a class called Person with an initialize method that takes a parameter name and sets it to an instance variable @name.
Ruby
Hint
Use class Person to start the class and def initialize(name) to define the constructor.
2
Add an instance method that returns self
Inside the Person class, add an instance method called show_self_instance that returns self.
Ruby
Hint
Define a method with def show_self_instance and return self.
3
Add a class method that returns self
Add a class method called show_self_class inside the Person class that returns self. Use the self. prefix to define the class method.
Ruby
Hint
Define a class method with def self.show_self_class and return self.
4
Create an instance and print results of both methods
Create a variable called person and set it to a new Person instance with the name "Alice". Then print the result of calling person.show_self_instance and Person.show_self_class.
Ruby
Hint
Use person = Person.new("Alice") to create the instance. Use puts to print the method results.
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
Step 1: Understand instance methods context
Inside an instance method, the code runs for a specific object created from the class.
Step 2: Identify what self points to
In this context, self refers to that specific object (instance) running the method.
Final Answer:
The current instance (object) of the class -> Option D
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
Step 1: Recall class method syntax
Class methods are defined by prefixing the method name with self. inside the class.
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.
Final Answer:
def self.my_method; 'hello'; end -> Option A
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
Step 1: Understand what self is inside speak
Inside the instance method speak, self is the instance fido.
Step 2: Evaluate self.class
Calling class on the instance returns its class, which is Dog.
Final Answer:
Dog -> Option A
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
This is a class method trying to call meow without specifying an instance.
Step 2: Understand method types
meow without self. is an instance method, so it needs an object to call it on.
Step 3: Identify the error
Calling an instance method directly inside a class method causes a NoMethodError because no instance is given.
Final Answer:
Calling instance method meow inside a class method without an instance -> Option B
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