ara.t.howard wrote: > > 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. Ara, Austin - thanks for the respsonses. Both are interesting approaches. However, I think I'm going to try digging into the opaque objects behind the scenes via DL and see if I can't do something more directly. Perhaps I'll contribute that back to evil.rb. :) Regards, Dan