self is a special variable that points to the object that “owns” the currently executing code.
self is the “current object” and the default receiver of messages (method calls) for which no explicit receiver is specified. (Which object plays the role of self depends on which context).
Inside an instance method:
class Ghost def reflect self end end g = Ghost.new g.reflect == g # => true
Inside a class or module method:
class Ghost def self.reflect self end end Ghost.reflect == Ghost # => true
Inside a class or module definition:
class Ghost self == Ghost # => true end module Mummy self == Mummy # => true end
selfwhen setting/getting instance attributes inside a class definition.
selfto denote a method within the class definition as a class method.
selfto reference the calling object within an instance method definition.