self / Class Methods / Instance Variables / Class Variables
Explains how to use Ruby's self keyword along with instance variables (@) and class variables (@@). self is also used to define class methods.
Syntax
class ClassName
# Class variable: shared across all instances.
@@class_variable = initial_value
def initialize
# Instance variable: holds data unique to each instance.
@instance_variable = value
end
# self inside an instance method: refers to the instance itself.
def instance_method
self # Returns the instance itself.
self.method_name # Calls a method on the instance itself.
end
# Defining a class method: write def self.method_name.
def self.class_method_name
process # Here, self refers to the class itself.
end
end
# Calling a class method.
ClassName.class_method_name
Reference
| Syntax | Description |
|---|---|
| self (inside an instance method) | Refers to the instance on which the method was called. |
| self (at the top level of a class definition) | Refers to the class itself. Used to define class methods. |
| def self.method_name | Defines a class method. Can be called as ClassName.method_name without an instance. |
| @variable_name | An instance variable. Holds data that belongs exclusively to each instance. |
| @@variable_name | A class variable. Shared among all instances of the same class. |
Sample Code
class Counter
# Class variable: shared across all instances.
@@total = 0
attr_reader :count, :name
def initialize(name)
@name = name # Instance variable: unique to this instance.
@count = 0
@@total += 1
end
def increment
@count += 1
self # Returning self enables method chaining.
end
def info
"#{@name}: #{@count} times (total instances: #{@@total})"
end
# Class method: can be called without an instance.
def self.total
@@total
end
# Class instance variable: safer than a class variable (stays independent under inheritance).
@description = "Counter class"
def self.description
@description
end
end
a = Counter.new("Counter A")
b = Counter.new("Counter B")
# Calling instance methods.
a.increment.increment.increment # Chaining works because increment returns self.
b.increment
puts a.info # Counter A: 3 times (total instances: 2)
puts b.info # Counter B: 1 time (total instances: 2)
# Calling class methods.
puts Counter.total # 2
puts Counter.description # Counter class
Notes
self refers to different things depending on context. Inside an instance method, it refers to the instance itself. At the top level of a class definition, it refers to the class itself. Returning self from an instance method enables method chaining.
Class variables (@@) are convenient but are shared with subclasses, which can lead to unexpected behavior. It is safer to use class instance variables instead — these are @ variables defined at the top level of a class and accessed inside class methods.
If you mistype an instance variable name inside an instance method (e.g., @nmae), Ruby will not raise an error — it will silently return nil. Be careful with typos, as they can make debugging difficult.
For the basics of class definitions, see class / initialize / attr_accessor. For modules and mixins, see module / include / extend.
If you find any errors or copyright issues, please contact us.