How does the method self.number_of_instances work what is the .number_of_instances for?


#1



How does the method self.number_of_instances work what is the .number_of_instances for? Thanks.

https://www.codecademy.com/en/courses/ruby-beginner-en-MFiQ6/1/4?curriculum_id=5059f8619189a5000201fbcb


class Person
  # Set your class variable to 0 on line 3
  @@people_count = 0
  
  def initialize(name)
    @name = name
    # Increment your class variable on line 8
    @@people_count += 1
  end
  
  def self.number_of_instances
    # Return your class variable on line 13
    return @@people_count
  end
end

matz = Person.new("Yukihiro")
dhh = Person.new("David")

puts "Number of Person instances: #{Person.number_of_instances}"


#2

self in this application is a reference to the class, itself. It returns the accumulated value of @@people_count, which is a class variable. The number of instances is the count of objects instantiated by this class. matz and dhh are instances.