The Blueprint and the House: Ruby Classes and Objects Explained
Very often I see new developers hit a massive brick wall when they start learning Ruby. You understand variables, you understand if/else statements, and you understand loops.
But then, a tutorial introduces the words "Class" and "Object". Suddenly, the code is full of @ symbols, def initialize, and .new. If you don't have a computer science background, this jargon feels incredibly intimidating.
You do not need a university degree to understand this. You just need a good mental model.
Here is the absolute simplest way to understand Classes and Objects in Ruby, without the heavy academic vocabulary.
The Mental Model: The Blueprint vs. The House
Imagine you are an architect. You sit down and draw a Blueprint for a house. The blueprint says the house will have 2 doors, 3 windows, and be painted a specific color.
Can you live inside a blueprint? Can you open the doors of a blueprint?
No. It is just a piece of paper. It is a set of instructions.
To actually live in it, you have to hire a builder to read the blueprint and construct a Real House out of wood and bricks. You can use that exact same blueprint to build 50 different houses on the same street. Some owners might paint their house red, and others might paint it blue, but they all came from the same instructions.
- The Class is the Blueprint.
- The Object is the Real House.
Let's look at how this translates into Ruby code.
1. Creating the Blueprint (The Class)
In Ruby, we write a Class to define the rules of our concept. Let's stick with our house example.
class House
# This is just the blueprint.
# It doesn't physically exist in our app's memory yet.
end
Right now, this code does absolutely nothing. It is just a piece of paper sitting on your desk.
2. Building the House (The Object)
To turn our blueprint into something real that we can interact with, we have to "build" it. In Ruby, we do this using the .new method.
# We are building two distinct physical houses from the same blueprint
my_house = House.new
your_house = House.new
When you type House.new, Ruby reads your class, allocates physical memory in your computer, and creates a living, breathing Object. my_house and your_house are two completely separate objects.
3. The Front Door (Initialize)
When a house is built, certain things need to happen immediately. The builder needs to paint it and give the owner the keys.
In Ruby, we use a special method called initialize. Think of this as the "Builder". Whenever you call .new, Ruby automatically looks for the initialize method and runs it first.
class House
def initialize(color)
puts "Building a brand new #{color} house!"
end
end
my_house = House.new("Red")
# Outputs: Building a brand new Red house!
4. The Backpack (Instance Variables)
Here is where beginners get confused. What is the @ symbol?
Imagine every Object wears a little backpack. Inside that backpack, it keeps its own private data. If my_house is Red, and your_house is Blue, they need a way to remember their own colors.
In Ruby, any variable that starts with an @ is an Instance Variable. This means it goes directly into the Object's backpack. It remembers this data forever.
class House
def initialize(color)
# Put the color into this specific object's backpack
@color = color
end
def what_color_am_i?
# Look inside the backpack and read the color
"I am a #{@color} house."
end
end
my_house = House.new("Red")
your_house = House.new("Blue")
puts my_house.what_color_am_i? # Outputs: I am a Red house.
puts your_house.what_color_am_i? # Outputs: I am a Blue house.
Notice how they don't get confused? my_house looked in its own backpack. your_house looked in its own backpack. They share the same blueprint, but they have their own unique memory.
5. The Actions (Methods)
Finally, Objects need to do things. A house has doors you can open. A user has a password they can reset. An invoice has a total it can calculate.
We define these actions by writing normal methods (def) inside the Class.
class House
def initialize(color)
@color = color
@door_open = false # Doors are closed by default
end
def open_door!
@door_open = true
puts "The door is now open."
end
end
my_house = House.new("Red")
my_house.open_door!
Summary
Don't let the computer science vocabulary intimidate you.
- Class: The written instructions (Blueprint).
- Object (or Instance): The actual thing built from those instructions (The House).
-
.new: The command to hire the builder and construct the object. -
initialize: The setup tasks the builder does on day one. -
@variable: The private backpack where the object remembers its own data. - Methods: The actions the object can perform.
Everything in Ruby is an object. A String is an object built from the String class blueprint. An Integer is an object built from the Integer blueprint. Once you grasp this simple idea, the entire Ruby language opens up and starts to feel like plain English.
Top comments (0)