From: Gary Wright Date: 2007-03-26T09:17:19+09:00 Subject: Re: class methods and instance variables in ActiveRecord::Base On Mar 25, 2007, at 2:45 PM, zig wrote: > OK, I think I've got it now. Class methods of the class have access > to class variables, class instance variables, but not instance > variables. Instance methods of the class have access to class > variables, instance variables, but not class instance variables. > Class variables are accessible to both, but neither class instance > variables nor instance variables are. Therefore class instance > variables are not the same thing as class variables. It is also important to realize that class variables ('@@xyz') are resolved lexically. They are not resolved relative to 'self' but instead relative to the surrounding lexical scope (top-level, file, module blocks, class blocks). This is very different than instance variables ('@xyz') which are always resolved relative to 'self'. class A @@example = 1 puts @@example # 1, lexically associated with A def self.example @@example end end puts @@example # error, lexically associated with top-level # but not defined yet. def A.top_level_example @@example # top level @@example!!!! end @@example = 2 puts A.top_level_example # 2, gets @@example relative to top-level puts A.example # 1, gets @@example relative to A Gary Wright