jeudi 26 février 2015

Is the `@count` an instance variable or class variable in Ruby?


class Counter
def self.counted_new
@count = 0 if @count.nil?
@count += 1
new
end

def self.count
@count
end
end


In format, @count looks like a instance variable, but when I load this in "irb"(interface ruby) and type four command code



Counter.counted_new
Counter.count
Counter.counted_new
Counter.count


@count finally became 2! acting like a class variable


Aucun commentaire:

Enregistrer un commentaire