From: zdennis Date: 2006-04-08T02:49:32+09:00 Subject: Re: Simple Question About Deleting Instances zdennis wrote: > Nathan Olberding wrote: >> zdennis wrote: >>> You are creating an instance variable @b on your class A. The class A is >>> an instance of the class Class, so it to can have instance variables. >> Not to be a whiner, but is there a reason we can't just do attr_accessor >> :@@var? What you're saying is logical, but it seems like we're getting >> from point A to point B via point F. > > No. You just need to understand how classes and instances of a class work. > > attr_* methods set instance variables and get/set methods on an instance > of a class. It does this using symbols. > > class A > attr_accessor :b > end > > :b will point to instance variable @b, on an instance of A. > > If you want to use attr_* to set class variables you need to work on the > instance of the class itself. That is why we do: > > class A > class << self > attr_accessor :class_var > end > end > > Because "class << self" opens up the instance of class A. And :class_var > will map to a '@class_var' instance variable on your class itself (and > not on an instance of your class). class A end # this creates an instance of your class, which is of class A a = A.new # this opens the instance of class A itself, which is of class Class class A class << self end end Zach