From: Josh Cheek Date: 2011-12-21T13:34:16+09:00 Subject: Re: Accessing class instance variables from an instance? --f46d0444805f8a5f5504b4929e3a Content-Type: text/plain; charset=ISO-8859-1 On Tue, Dec 20, 2011 at 9:42 PM, Khat Harr wrote: > Actually, now that I'm thinking about it the existing behavior sort of > what makes more sense. I'd think that subclasses would be better > instantiating their own class vars rather than expecting a unique copy > of the superclass var. I can see why you'd want the class instance vars > rather than modifying the existing mechanic. On the other hand, maybe a > new variable type to encapsulate the behavior of a class instance var in > the way that it's being used here would be something worth discussing? > > -- > Posted via http://www.ruby-forum.com/. > > I don't see any point in that, class ivars seem sufficient to me. What would be nice, though, is an attr_accessor equivalent that defines methods for both the class and instance. (overly simple example) class Class def whatever_accessor(meth_name) delegate_to_class meth_name singleton_class.instance_eval do attr_accessor meth_name end end def delegate_to_class(meth_name) define_method meth_name do |*args, &block| self.class.send meth_name, *args, &block end end end class Foo whatever_accessor :bar self.bar = :baz end Foo.new.bar # => :baz --f46d0444805f8a5f5504b4929e3a--