Been reading a tutorial ruby book, and i’m not sure if this error is
caused by a typo in the book, or some logical error only the experienced
would know how to correct
/code//
class Square
def intialize
if defined?(@@number_of_squares)
@@number_of_squares += 1
else
@@number_of_squares = 1
end
end
def self.count
@@number_of_squares
end
end
a = Square.new
b = Square.new
puts Square.count
///
Here’s the error:
///
ruby Square.rb
Square.rb:11:incount': uninitialized class variable @@number_of_squares in Square (NameError) from Square.rb:17 Exit code: 1 ruby Square.rb Square.rb:10: undefined local variable or method
s’ for Square:Class
(NameError)
Exit code: 1
ruby Square.rb
Square.rb:11:in `count’: uninitialized class variable
@@number_of_squares in Square (NameError)
from Square.rb:17
Exit code: 1
The triple slashes(///) aren’t part of the code, just to make reader
friendly
Many thanks in advanced.