I have this code:
class Person
@@instance_count = 0
def initialize name
@name = name
@@instance_count += 1
end
def self.instance_count
p "Instances created - #{@@instance_count}"
end
end
person_1 = Person.new "first_name"
person_2 = Person.new "second_name"
person_3 = Person.new "third_name"
Person.instance_count
which outputs "Instances created - 3".
I do not understand why += in initialize increments @@instance_count and not resets it to 0 every time a new instance variable is created. What is happening when @@instance_count is not reset to 0 every time a new instance is created?
@@var) with instance variables (@var)