Ruby Class Keyword
last modified October 18, 2026
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. Defining an existing class again reopens it, adding or replacing methods. Classes encapsulate data and behavior into reusable components. They support inheritance and mixins.
Ruby classes can contain methods, constants, and nested classes. Objects are instances of classes, and classes themselves are objects.
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 must be constants, so they start with an uppercase letter.
Class with Initialize Method
The initialize method is the constructor in Ruby. It is called
when new creates an instance. 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 < syntax. 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 class first, then in its ancestors
(included modules and superclasses).
Class Variables and Methods
Class variables (@@) are shared by the class, its instances, and
its subclasses. Class methods are defined with a self. prefix. This
example tracks the 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}"
Each new instance increments @@count. 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_writer creates a
setter, and attr_accessor creates both. Here title is
read-only, while author can be read and 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. Ruby does not support multiple inheritance, but
mixins provide a similar way to share behavior.
Singleton Classes
Ruby allows defining methods on individual objects using singleton classes.
A method defined with def obj.name is stored in that object's
singleton class. 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
Ruby Classes and Modules Documentation
This tutorial covered Ruby's class keyword with examples demonstrating core OOP features like inheritance, mixins, and singleton classes.
Author
List all Ruby tutorials.