From: "ara.t.howard" Date: 2007-05-20T03:38:34+09:00 Subject: Re: Simulating single assignment in Ruby On May 19, 2007, at 10:46 AM, Sven Suska wrote: > ara.t.howard schrieb: > >> class Class >> def fattr a, &b >> define_method(a){ instance_eval &b } >> end >> end >> >> class C >> fattr(:a){ 40 } >> fattr(:b){ a + 2 } >> end >> c = C.new >> p c.b # => 42 >> with this approach there is simply no variable to modify - it >> exists only through closure. > > But a method to modify: > class C > fattr(:b){ 17 } > end > p c.b # => 17 > > Or did I get something wrong? > > Sven > yes of course. and we also have 'remove_const :C' which allows us to replace the class wholesale, and this line of reasoning extends all the way up in ruby. still - my approach does, in fact, prevent modifying instance vars; the fact that the class itself can stiff be modified (by adding methods) may or may not be an issue. perhaps enclosed attributes and C.freeze and (class << C;self;end).freeze would be sufficient. -a -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama