Object-Oriented Programming Basics

Lesson 14 of 20

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.

Classes Are Blueprints

class Dog defines the shape; Dog.new builds real objects from it.

initialize Sets Up

new always calls initialize — perfect for storing starting state in @variables.

@ Means Instance State

@name lives inside one object; each instance keeps its own copy, private to the outside.

attr_accessor

One line auto-writes getter and setter methods, opening a field up to the world.