class variable works like this:
class Hello
@@x = 0
def self.counter
@@x
end
def initialize
@@x += 1
end
end
Hello.new
Hello.new
Hello.new
p Hello.counter
#=> 3
but class instance variable doesn't:
class Goodbye
@x = 0
def self.counter
@x
end
def initialize
@x += 1
end
end
Goodbye.new
Goodbye.new
Goodbye.new
Goodbye.new
p Goodbye.counter
#=> Error
What am I doing wrong ? I was under impression that class instance variables are same as class variables, just without inheritance problem ... but how do you use them (for example, to count instances of specific class like in code I posted here) ?