Understanding the Self Keyword in Ruby
📖 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