From: aurelianito Date: 2005-10-11T05:46:53+09:00 Subject: Re: Method added hook > > I want more granularity than what the five (0-4) $SAFE levels offer. I > > want to be able to say "can write in file A but not in file B" (...) > Regarding the method hooks, one thing that you can do to complicate things > somewhat is to do something like this (not tested): > > class SomeClass > def some_method(*a, &b) > # ... > end > > alias_method :__safety__, :some_method > > def self.method_added(sym) > unless @my_add > # Revert back to original > if sym == :some_method > alias_method :some_method, :__safety__ > end > else > @my_add = false > end > end > end I've patched it a little bit and it worked nicely. class Klass def foo puts "foo" end alias_method :__safety__, :foo def self.method_added(sym) if sym == :foo && ! caller.find { |call| /method_added/ =~ call } alias_method :foo, :__safety__ end end end class Klass def foo puts "evil" end end a = Klass.new a.foo // prints "foo" Do you know if this solution is thread safe or not?, Aureliano.