class / initialize / attr_accessor
The basic syntax for defining classes in Ruby. Covers class definition, instance creation, the initialization method, and automatic accessor generation.
Syntax
# Define a class.
class ClassName # Must start with an uppercase letter (PascalCase)
# Initialization method (runs automatically when new is called).
def initialize(arg1, arg2)
@instance_variable1 = arg1
@instance_variable2 = arg2
end
# Define an instance method.
def method_name
# process
end
end
# Create an instance.
object = ClassName.new(arg1, arg2)
# Automatically generate accessor methods.
attr_reader :variable_name # Read-only (getter only)
attr_writer :variable_name # Write-only (setter only)
attr_accessor :variable_name # Read and write (getter + setter)
Method List
| Syntax / Method | Description |
|---|---|
| class ClassName | Defines a class. The class name must use PascalCase (first letter uppercase). |
| def initialize | The initialization method that runs automatically when an instance is created (when new is called). |
| @variable_name | An instance variable. Accessible from any method within the same instance. |
| attr_reader | Automatically generates a read-only accessor for the specified instance variable. |
| attr_writer | Automatically generates a write-only accessor for the specified instance variable. |
| attr_accessor | Automatically generates both read and write accessors for the specified instance variable. |
| ClassName.new | Creates a new instance. Any arguments are passed directly to initialize. |
Sample Code
# Basic class definition.
class Person
# Use attr_accessor to automatically generate getters and setters.
attr_accessor :name, :age
# Use attr_reader to generate a read-only accessor.
attr_reader :id
# A class variable shared across all instances.
@@count = 0
def initialize(name, age)
@name = name
@age = age
@@count += 1
@id = @@count # Assign an ID in creation order.
end
def introduce
"My name is #{@name}. I am #{@age} years old."
end
def self.count
@@count
end
end
# Create instances.
alice = Person.new("Alice", 30)
bob = Person.new("Bob", 25)
puts alice.introduce # My name is Alice. I am 30 years old.
puts bob.introduce # My name is Bob. I am 25 years old.
# Use the getter and setter provided by attr_accessor.
puts alice.name # Alice
alice.name = "Alicia"
puts alice.name # Alicia
# You cannot assign to an attr_reader variable.
puts alice.id # 1
# alice.id = 99 # Raises NoMethodError.
# Check the total number of instances using a class method.
puts Person.count # 2
Notes
Ruby classes are very straightforward to define. Using attr_accessor automatically generates getter and setter methods, eliminating the need to write them by hand. You can also specify multiple variables at once (e.g., attr_accessor :name, :age, :email).
Instance variables (@variable) belong only to their own instance and are not shared with other instances of the same class. Class variables (@@variable), on the other hand, are shared across all instances.
Class variables (@@variable) are also shared with subclasses that inherit from the class. This can lead to unexpected bugs due to unintended sharing. For data shared across classes, it is safer to use class instance variables (using @variable inside a class method).
For access control (public/private/protected), see public / private / protected. For details on self, instance variables, and class variables, see self / @variable / @@variable.
If you find any errors or copyright issues, please contact us.