From: "David A. Black" Date: 2009-07-04T21:56:01+09:00 Subject: Re: How to remove/delete/destroy class variable in Ruby 1.9? Hi -- On Sat, 4 Jul 2009, Robert Klemme wrote: > On 04.07.2009 14:06, David A. Black wrote: >> On Sat, 4 Jul 2009, dzw! wrote: >> >>> Ok, code below shows better what I am talking about, I understand that >>> @@var is acting like global variable. >>> But how to get rid of it when is not needed anymore? > >> You're trying to maintain and alter state on a per-class basis, which >> really means on a per-object (class object) basis. Class variables are >> not suited to that purpose; they are, as you say, really sort of >> vertical globals. >> >> (In fact, I've always thought that $$var would be a better designation >> for class variables than @@var, because @@var makes it look like class >> variables have some kind of affinity with instance variables, which >> they definitely don't.) > > The only commonality I can think of off the top of my head is the fact that > you can use them from within instances much the same way as instance > variables. But you're always within an instance of something. You can access global variables there too :-) This is also why I don't like the "cattr_*" idiom (cattr_reader and so on), or at least its naming. "Attribute" suggests an attribute of a particular object. Class variables don't represent per-object state, so they're not suited to "attribute" implementation. >> Anyway -- I don't know of any way to undefine a class variable. I'd >> recommend using instance variables if you need per-class state. > > My 0.02 EUR: the best way to get rid of class variables is to not use them in > the first place. :-) I agree. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) Training! Intro to Ruby, with Black & Kastner, September 14-17 (More info: http://rubyurl.com/vmzN)