Twice the @, twice as Classy Error


#1



I don't understand this tutorial 

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 += 0
  end
  
  def self.number_of_instances
    # Return your class variable on line 13
    @@people_count == 0
  end
end

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

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

Can you HELP ME PLEASE !!!!


#2

Hi @geoffroyh,

what don't you understand?


#3

The code doesn't work


#4

`

class Person
# Set your class variable to 0 on line 3
@@people_count = 0 <- put this

def initialize(name)
@name = name
# Increment your class variable on line 8
@@people_count += 1 <- put this
end

def self.number_of_instances
# Return your class variable on line 13
@@people_count <- put this
end
end

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

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


#5

Posting working code with no explanation is against community guidelines,

:no_entry:


#6

here you are to set the people_count variable to 0, you cannot incremnt a variabe that hasn't yet been declared

to,

@@people_count = 0


to,

@@peoplecount += 1_


simply put,

@@people_count


#7

You need to add 1 to your number of instances on line 8, that is how it will increment from 0 to 1 to 2 to 3 as it goes through the code.


#8

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