From: David Masover Date: 2009-02-09T06:42:14+09:00 Subject: Re: best way to protect class instance variables Robert Dober wrote: > On Sun, Feb 8, 2009 at 9:48 PM, David Masover wrote: > What about fine graining the control to the closure? > Doesn't really matter, so long as it can be read and duplicated satisfactorily to fool the rest of the module. And your method doesn't quite work -- I assume you were wanting blah to return something with [] methods that restrict access to the scope'd blah? class MyBlah blah = [0,1] @blah = Object.new @blah.extend(Module.new do define_method :[] { |i| blah[i] } define_method :[]= do |idx, value| do_checks blah[idx] = value end end) class << self attr_reader :blah end end Of course, this prevents the internal array from being modified, so I see the point -- if there are other methods sharing the same scope. Even then, it's still possible to obliterate the whole thing, it's just not as easy to change what a method does by changing the array out from under it. Interesting exercise -- still going to recommend that you don't do this without good reason. It's not sufficient to prevent evil things if you eval untrusted code, and it's complete overkill versus just having good documentation and conventions.