What does self.number_of_instances relate to? All I see is a class called person. How am I supposed to read self.number_of_instances?


#1



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

what does self.number_of_instances relate to? All I see is a class called person. How am I supposed to read self.number_of_instances?

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.number_of_instances is a class method that returns the value of a class variable, @@people_count which accumulates by one with each new instance.. self is the class, itself. The method is called on the class, not the instances, hence, Person.number_of_instances.


#3

Is Person also the class? And what is number_of_instances? Thanks.


#4

Person is defined as a class. The classs variable @people_count is incremented by 1 each time a new instance of the class is created. This is akin to keeping a running count of the number of people registered in the class. number_of_intstances is the class method for retrieving the people count.