From: William Paulson Date: 2012-06-10T05:40:05+09:00 Subject: Re: Class variables and self On Jun 9, 2012, at 2:43 PM, Mike Glaz wrote: > I'm currently reading The Well-Grounded Rubyist. In the section about > class variables, instance variables, and self they have this class as an > example: > > class Car > @@makes = [] > @@cars = {} > > attr_reader :make > > def self.total_count > @total_count || 0 > end > > def self.total_count=(n) > @total_count = n > end > > def self.add_make(make) > unless @makes.include?(make) > @@makes << make > @@cars[make] = 0 > end > end > > def initialize(make) > if @@makes.include?(make) > puts "Creating a new #{make}!" > @make = make > self.class.total_count += 1 > else > raise "No such make: #{make}." > end > end > > def make_mates > @@cars[self.make] > end > end > > My question is in the initialize method about the line: > > self.class.total_count += 1 > > what is the purpose of the 'class' in that statement? > Since total_count is a method of class Car, inside the initialize method of an instance you can refer to class Car as self.class. It would seem simpler to say Car::total_count in this particular case.