From: Robert Klemme Date: 2005-04-22T23:05:10+09:00 Subject: Re: accessor for Class Variable "Lyndon Samson" schrieb im Newsbeitrag news:a39f6ad0050421082652ed737e@mail.gmail.com... > On 4/22/05, Robert Klemme wrote: > > > > > > Class variables are really only useful if you want to access them from > > instances and from the class in a similar way (via "@@var_name"). > > Otherwise they have more drawbacks than pros so I avoid to use them > > whenever I can. (Can't really remember a case where I actually needed > > them.) > > > What about situations like the following ( taken from an attempt at an > old rubyquiz ) > > class Op > @@lookup = {} > def Op.register(newOp, newOpClass) > @@lookup[newOp] = newOpClass > end > end > > class AddOp < Op > .. > end > class SubOp < Op > .. > end > class MulOp < Op > .. > end > class DivOp < Op > .. > end > > Op.register("+", AddOp) > Op.register("-", SubOp) > Op.register("*", MulOp) > Op.register("/", DivOp) > > They are not really OO 'clean' but better than globals surely? Yes, but you still don't need a class var - a standard instance var of the class is sufficient, as you can see: class Op @lookup = {} def self.register(newOp, newOpClass) @lookup[newOp] = newOpClass end end .... and even saves two chars typing that I could use to replace "Op" by "self" in the method definition :-))) Kind regards robert