Classes, objects, initialize and attr_accessor: build your own blueprints and stamp out objects that carry state of their own.
The big picture
Blueprints and the objects they build
A class is a blueprint; new stamps out real objects from it, each carrying its own @state and answering the same method calls.
1
Classes Are Blueprints
class Dog defines the shape; Dog.new builds real objects from it.
2
initialize Sets Up
new always calls initialize — perfect for storing starting state in @variables.
3
@ Means Instance State
@name lives inside one object; each instance keeps its own copy, private to the outside.
4
attr_accessor
One line auto-writes getter and setter methods, opening a field up to the world.
Blueprints and the objects they build
Classes Are Blueprints1class Dog defines the shape; Dog.new builds real objects from it.
initialize Sets Up2new always calls initialize — perfect for storing starting state in @variables.
@ Means Instance State3@name lives inside one object; each instance keeps its own copy, private to the outside.
attr_accessor4One line auto-writes getter and setter methods, opening a field up to the world.
4 building blocks make up Blueprints and the objects they build
Watch it run
From blueprint to object
Watch a Dog get built by new and initialize, speak, and then get renamed through its setter.
dog.rb
1class Dog
2attr_accessor:name
3
4definitialize(name)
5 @name = name
6end
7
8def speak
9"#{@name} says Woof!"
10end
11end
12
13dog = Dog.new("Rex")
14puts dog.speak
15dog.name ="Max"
16puts dog.speak
main
class Dog opens a blueprint — Ruby executes the class body right away.
Memory
no variables yet — press play
Console
output appears as the program runs…
Step 1/9
In 3D
The call stack: new → initialize → speak
Every method call pushes a frame onto the stack and every return pops it. Play the script, then drag to orbit.
Drag to orbit the scene. Press Play to watch memory in action.
Practice
Match the OOP tools
Click a term, then click its description to lock the pair.
5 conceptsTap a card to expand
?class Dogdrops into place
?@namedrops into place
?Dog.newdrops into place
?attr_readerdrops into place
?selfdrops into place
Challenge
Predict the output
A counter that bumps itself — watch what bump returns.
counter.rb
1class Counter
2attr_accessor:count
3
4def initialize
5 @count =0
6end
7
8def bump
9 @count = @count +1
10end
11end
12
13c = Counter.new
14puts c.bump
15puts c.bump
16puts c.count
Your predicted output — one line per console line
Hint: bump stores the new value in @count, and in Ruby an assignment expression itself evaluates to the assigned value — so each puts prints the freshly updated count.
Recap
Key takeaways
Key takeaways5 takeaways
01
class defines the blueprint; Dog.new builds real objects from it.
02
new always calls initialize — set up @variables there.
03
@name is instance state, private to each individual object.
04
attr_accessor writes getter and setter methods for you in one line.
05
Same method, different objects: each answers from its own state.