0
class A
  @@var = "X"

  def self.class_method
    puts "A.class_method: #{@@var}"
  end
end

class B
  @var = "Y"

  def self.class_method
    puts "B.class_method: #{@var}"
  end
end

A.class_method
B.class_method

I'm a little confused here. It seems @var is acting like a class variable instead of an instance variable. What is the explanation to this behaviour?

Leantraxxx
  • 4,506
  • 3
  • 38
  • 56
  • 2
    See [Ruby class instance variable vs. class variable](http://stackoverflow.com/q/15773552/567863) – Paul Fioravanti Apr 16 '16 at 23:49
  • 2
    What you're seeing is that `@var` is an instance variable on the _class_ B itself, not an an _instance_ of B. Check `B.instance_variables` then check `B.new.instance_variables`. – Michael Berkowski Apr 16 '16 at 23:50

0 Answers0