Class Variables

If we needed each philosopher to keep track of his or her creation (or graduation) date, it would be a fairly simple task.

class Philosopher
  def initialize
    @graduation_date = Time.now
  end
  attr_reader :graduation_date
end

But suppose we also thought that each philosopher should come stamped with a serial number. The initialize method would assign a value to an instance variable, say @serial, and that number would increment with each new graduate who walked out the door.

It turns out that we'll need a new kind of variable to get the job done, because somehow the academy always has to keep track of what the next serial number should be. Surely this isn't a job for an instance variable; we only want one copy of this information, ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.