From: "Ara.T.Howard" Date: 2005-07-12T07:59:03+09:00 Subject: Re: class_attr_accessor On Tue, 12 Jul 2005, Jeffrey Moss wrote: > I think it only gets tricky like you've pointed out if you're trying to do > default variable initialization values, but on the other hand this is > probably 99% of the useful purposes of a class_attr is with a default value > set. So your point is well taken. right you are. it bit me a few times and was ending up writing things like class A class << self attr 'a' attr 'b' def init self.a = 42 self.b = 'forty-two' end def inherited subklass subklass.init super end end self.init end class B < A self.b = 42.0 end all over the place. which starts being kinda ugly. i found that i almost always wanted class varibles to essentially call 'super' when not over-ridden - in otherwords to get a reasonable value from the parent class - but this is not possible with either @@vars or @vars without some trickery. so i wrote it once and called it traits. ;-) btw. one of the things i like most about traits is that combination getter/setter reads so well for exactly the case of class inheritence/default values: require 'traits' class Package class << self trait 'size' => 42 trait 'weight' => 42 end end class LightPackage weight 4.2 end class HeavyPackage weight 420 end and this works like it looks - all still have size 42. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================