Ruby - Using Class method to access class variable

Description

Using Class method to access class variable

Demo

class Square 
  def initialize 
    if defined?(@@number_of_squares) 
      @@number_of_squares += 1 #   w w  w.  j av a2 s  . c o  m
    else 
      @@number_of_squares = 1 
    end 
  end 

  def Square.count 
    @@number_of_squares 
  end 
end 

a = Square.new 
puts Square.count 
b = Square.new 
puts Square.count 
c = Square.new 
puts Square.count

Result

Related Topic