What's the Difference Between @variable and @@variable in Ruby?

In Ruby, variables with a single at-sign (i.e. @variable) are class instance variables while variables with double at-sign (i.e. @@variable) are class variables. This means that:

For example:

class Person
  @@total = 0

  def initialize(name)
    @name = name
    @@total += 1

  def self.total

  attr_accessor :name

person1 = Person.new("Bob")
person2 = Person.new("John")

puts "#{person1.name} & #{person2.name} (#{Person.total})"

# output: "Bob & John (2)"

Hope you found this post useful. It was published . Please show your love and support by sharing this post.