Ruby and class variables in inherit class
问题 class A def set(v) @@v = v end def put puts @@v end end class B < A end class C < A end B.new.set 'b' B.new.put # => b C.new.set 'c' C.new.put # => c B.new.put # => c Why? And how should I write this to have 'b' in last B.new.put? 回答1: Here is a nice article on the subject - Class and Instance Variables In Ruby. Basically, what you can do is: class A class << self attr_accessor :class_var end def set_class_var(value) self.class.class_var = value end def get_class_var self.class.class_var end