Ruby Class Keyword
last modified April 27, 2025
This tutorial explains how to use Ruby's class keyword to create
object-oriented programs. Classes are fundamental building blocks in Ruby.
The class keyword defines a new class in Ruby. Classes encapsulate data and behavior into reusable components. They support inheritance and mixins.
Ruby classes can contain methods, constants, and other class definitions. Everything in Ruby is an object, and classes define object blueprints.
Basic Class Definition
This example shows the simplest class definition in Ruby. The class contains a single method that outputs a greeting.
class Greeter
def greet
puts "Hello, Ruby!"
end
end
g = Greeter.new
g.greet
The class keyword starts the definition, followed by the class name.
We create an instance with new and call its greet method.
Class names should be constants (start with uppercase).
Class with Initialize Method
The initialize method serves as the constructor in Ruby classes.
It runs when a new instance is created. This example customizes the greeting.
class Person
def initialize(name)
@name = name
end
def greet
puts "Hello, #{@name}!"
end
end
p = Person.new("John")
p.greet
The initialize method takes a name parameter. Instance variables
(@name) store object state. We pass arguments to new
which forwards them to initialize.
Class Inheritance
Ruby supports single inheritance using the < operator. This
example shows a base class and a derived class sharing behavior.
class Animal
def speak
puts "Animal sound"
end
end
class Dog < Animal
def speak
puts "Woof!"
end
end
a = Animal.new
a.speak
d = Dog.new
d.speak
Dog inherits from Animal but overrides the speak
method. Ruby looks up methods in the current class first, then parent classes.
Class Variables and Methods
Class variables (@@) are shared among all instances. Class methods
are defined with self. prefix. This tracks instance count.
class Counter
@@count = 0
def initialize
@@count += 1
end
def self.total
@@count
end
end
3.times { Counter.new }
puts "Total instances: #{Counter.total}"
@@count increments with each new instance. The class method
total returns the count. Class methods are called on the class itself.
Accessor Methods
Ruby provides shortcuts for getter/setter methods. attr_reader,
attr_writer, and attr_accessor simplify attribute access.
class Book
attr_reader :title
attr_accessor :author
def initialize(title, author)
@title = title
@author = author
end
end
b = Book.new("Ruby Guide", "Jane Doe")
puts b.title
b.author = "John Smith"
puts b.author
attr_reader creates a getter, attr_accessor creates
both. The title is read-only while author can be changed.
Modules as Mixins
Ruby classes can include modules to gain additional functionality. This example shows a module mixed into a class.
module Loggable
def log(message)
puts "[LOG] #{message}"
end
end
class Product
include Loggable
def initialize(name)
@name = name
log "Created product: #{@name}"
end
end
p = Product.new("Laptop")
The Product class gains the log method from the
Loggable module. Mixins provide multiple inheritance-like features.
Singleton Classes
Ruby allows defining methods on individual objects using singleton classes. This example adds a method to one specific instance.
class Dog
def bark
puts "Woof!"
end
end
d1 = Dog.new
d2 = Dog.new
def d1.special_bark
puts "Special woof!"
end
d1.bark
d1.special_bark
d2.bark
# d2.special_bark # Would raise NoMethodError
d1 gets an additional method not available to other Dog
instances. Singleton classes are useful for object-specific behavior.
Source
This tutorial covered Ruby's class keyword with examples demonstrating core OOP features like inheritance, mixins, and singleton classes.
Author
List all Ruby tutorials.