What are classes, objects, and methods in Ruby?
Updated Feb 20, 2026
Short answer
In Ruby, a class is a blueprint that defines data and behavior, an object is an instance created from that blueprint, and methods are actions objects can perform. Together, they form Ruby’s object-oriented model, where almost everything is represented as an object.
Deep explanation
Classes: blueprints for objects
A class defines the structure and behavior that objects created from it will have. It can contain attributes (data) and methods (behavior). In Ruby, classes are created with the class keyword.
For example, a User class might describe what every user object should store and what actions every user can perform:
class User def initialize(name) @name = name end
def greet "Hello, #{@name}" endendThe initialize method is a special method that runs when a new object is created. The variable @name is an instance variable, meaning each object gets its own copy of that data.
Objects: actual instances
An object is a specific instance of a class. The class describes what an object can do, while the object holds the actual values.
alice = User.new("Alice")puts alice.greetHere, User is the class, and alice is an object created from that class. Multiple objects can come from the same class while keeping different state.
A class is the recipe; an object is the finished dish created from that recipe.
The relationship looks like this:
Methods: behavior attached to objects
A method is a named piece of behavior that an object can execute. Methods are similar to functions in other languages, but in Ruby they belong to objects.
Ruby uses message passing: instead of directly changing an object’s internals, code usually calls methods on that object.
class BankAccount def deposit(amount) @balance += amount endend
account.deposit(100)The call account.deposit(100) sends the deposit message to the account object.
How they work together
| Concept | Purpose | Example |
|---|---|---|
| Class | Defines a blueprint | BankAccount |
| Object | Holds real data | account |
| Method | Performs an action | deposit |
Ruby follows the idea that objects combine data and behavior together. This makes code easier to organize because related information and actions live in one place.
⚠️ A useful interview shortcut: classes define what objects are, objects represent actual things, and methods describe what those things can do.
Ruby also supports inheritance, where one class can reuse or extend another class’s behavior. However, good Ruby design often favors small, focused objects rather than deep inheritance trees.
Real-world example
Imagine an online store. A Product class can describe every product, while each product object represents a specific item.
class Product def initialize(name, price) @name = name @price = price end
def discount(percent) @price -= @price * percent / 100 endend
laptop = Product.new("Laptop", 1000)laptop.discount(10)Here, Product is the blueprint, laptop is one object, and discount is a method that changes the object’s data.
Objects allow different products to share the same behavior while keeping their own values. A store can create thousands of product objects from one class without rewriting the same logic.
Common mistakes
- * **Class vs object** - Confusing the blueprint with the instance
- remember that `Car` describes a type while `my_car` is a specific object.
- * **Ignoring object state** - Treating objects like simple containers
- use instance variables and methods to manage related data.
- * **Using methods incorrectly** - Writing large methods that do too many things
- keep object behavior focused and readable.
- * **Overusing inheritance** - Creating complicated class hierarchies
- prefer simpler objects and composition when possible.
Follow-up questions
- What is the difference between a class method and an instance method?
- How does initialize work in Ruby?
- Is everything in Ruby an object?
- What is inheritance in Ruby?