From: Mark Hubbart Date: 2005-04-15T03:02:47+09:00 Subject: Re: class locals as class methods On 4/14/05, Trans wrote: > So I find myself once again creating some classes that have > attributes/state. I have two options, either the class defines > a class method for the info, or in a superclass I define a method > that will set that info to a class var when called. [...] > as that just feels natural. But then I have to go back and fix > it, of course. So last night it hits me, why not? Why not have > class locals define class methods? So in this case, Example C > would basically be like doing Example A. Yes, I know there's > namespace overlap, but class locals are rare enough that I don't > think that's a show stopper. Maybe there are other problems I'm > not seeing, perhaps there's some OOP formalism that this rubs the > wrong way. I don't know. But I've given it more than "15 minutes" > and it's actually growing on me. Am I crazy? I think meta-programming would suffer. # a Foo that is sort of like a Bar class FooBar < Foo bar_methods = Bar.instance_methods - self.instance_methods bar_methods.each{ ... } end FooBar.bar_methods => ????? It would be like penalizing people for using metaprogramming techniques :( Perhaps, if you just want to set attributes: class Class def set(name, value) self.class_eval "def self.#{name}() @@#{name} end" self.class_eval "@@#{name} = ObjectSpace._id2ref(#{value.object_id})" end end ==>nil class Foo set :foo, "yep!" end ==>"yep!" Foo.foo ==>"yep!" I'm sure it could be condensed even further, if needed. Off topic now, I thought there was an equivalent of #instance_variable_set for class variables? It would seem not. I wonder why. cheers, Mark