Classes are blueprints, not objects

Free preview 3 min to read

One definition, many objects

The "Ruby variables & assignment" course built a small BankAccount class to explain local versus instance variables, and quietly used it without ever explaining what class and .new do. This course closes that gap.

class BankAccount
  def initialize(starting_balance)
    @balance = starting_balance
  end

  def balance
    @balance
  end
end

account1 = BankAccount.new(100)
account2 = BankAccount.new(500)

class BankAccount ... end doesn't create an account. It defines what an account is: a blueprint describing what every BankAccount will have and be able to do. BankAccount.new(100) is the line that builds one, a real object in memory, following that blueprint. Calling .new a second time with a different number builds a second, completely separate object, account2, from the exact same blueprint.

Verified: two objects, two identities

account1.equal?(account2)
# => false, two distinct objects, even built from the same class

account1.class
# => BankAccount

BankAccount.class
# => Class

account1 and account2 are not the same object just because they came from the same class, .equal? checks real object identity, not resemblance, and it says so. And there's a small but important distinction worth noticing here: account1.class is BankAccount, but BankAccount.class is Class. A class is itself an object, an instance of Ruby's built-in Class class, whose entire job is producing other objects when .new is called on it.

A blueprint you've already been reading

This series' own standing example, Article, used since the very first Rails Core course as a hypothetical ActiveRecord model (there is no real articles table or Article class anywhere in this actual app; it's a reused teaching example, not this app's own, more complex, namespaced models), has always looked like this:

class Article < ApplicationRecord
end

That's the exact same class ... end syntax, on a class that has been called, read, and reasoned about for several courses already, Article.new(title: "Hello Rails") in the "Rails console (IRB)" course being one direct example. Nothing about that syntax was ever Rails-specific magic, it's the plain Ruby mechanism this course now opens up. The < ApplicationRecord part is a different piece, inheritance, saved for the very next course. This one stays focused on the class itself: what's inside it, and what .new does with it.

What this course covers

  1. Classes Are Blueprints, Not Objects: class/end, and what .new builds (you are here)
  2. The initialize Method: what .new calls automatically, and why
  3. Every Object Remembers Its Own State: instance variables, one private copy per object
  4. Reader and Writer Methods, Written by Hand: exposing an object's state on purpose
  5. attr_reader, attr_writer, attr_accessor: generating those same methods instead of typing them
  6. The Convenience ActiveRecord Was Hiding All Along: connecting this back to Article
  7. Putting It Together: writing a full class from scratch, and pointing at inheritance next

By the end, class Article < ApplicationRecord will read as ordinary Ruby with one unexplained word left in it, and that word is where the next course picks up.

Lesson 1 of 7 · free

Keep going with lesson 2

The initialize method

.new calls initialize automatically with its arguments, which work like any method's parameters, including defaults and ArgumentError.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...