From: Trans Date: 2005-04-15T03:49:35+09:00 Subject: Re: class locals as class methods Mark Hubbart wrote: > 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 :( Hmm... I would think that code would work just fine. I'm not suggesting that local vars be displaced, if that is what you mean, only that they overlap. So a class local var is a class method is a class local var. It is the same namespace afterall (i.e. self resoves to the same thing) so that's okay too. Am I understanding your example? For reference I tried: module Bar def a ; ; end def b ; ; end def c ; ; end end class Foo def x ; ; end def y ; ; end def z ; ; end end class FooBar < Foo # local foo_bar = Bar.instance_methods - self.instance_methods p foo_bar # class def self.bar_methods Bar.instance_methods - self.instance_methods end end p FooBar.bar_methods with the result: ["c", "b", "a"] ["c", "b", "a"] > 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. Sure, that's one fair way. There are a number of ways to do it, like the simple first examples I gave. But all of them seem, well, contrived. In other words, there's no sort of obvious standard to this kind of thing. And worse to me, the solutions all look like they've been hit up with an ugly stick ;-) > Off topic now, I thought there was an equivalent of > #instance_variable_set for class variables? It would seem not. I > wonder why. Hmm... I don't know, maybe b/c this is less characters? Klass.class_eval { @@x = "foo" } Thanks, T.