What is the purpose of the self keyword in Ruby?
The self keyword in Ruby refers to the current instance of the class. It is used to access instance variables and methods within the class.
For example:
class Person
attr_accessor :name
def initialize(name)
@name = name
end
def say_hello
puts “Hello, my name is #{self.name}”
end
end
person = Person.new(“John”)
person.say_hello
# Output:
# Hello, my name is John