On May 17, 2007, at 6:10 PM, Daniel Berger wrote: > Hi, > > Is there a way to freeze instance variables in Ruby in order to > make them single assignment? I'm just wondering if there's a way to > simulate Erlang in this regard as a way to avoid side effects. > > I don't want to simply not define (or undefine) a setter method > because you could still get at the instance variable via > instance_variable_set. Redefining instance_variable_set won't work > either, because that method is apparently not called when > performing direct assignment of instance variables. I tried calling > the freeze method on the instance variables directly but that > didn't seem to work either. > > Is it possible? > > Regards, > > Dan > cfp:~/src/ruby/ > cat a.rb 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 cfp:~/src/ruby/ > ruby a.rb 42 with this approach there is simply no variable to modify - it exists only through closure. i know someone could hack in some crazy Binding.of_caller thing to munge the closure, but it would be very painful. kind regards. -a -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama