Where did the method .number_of_instances come from?


#1



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


no error message.


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

It returns the class variable amount accumulated with each time an instance is created.

It is a class method so invokes on the self object named in the context,

Person.number_of_instances

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.