From: Joel VanderWerf Date: 2004-05-26T08:52:04+09:00 Subject: Re: $SAFE idiom (Re: defining condititions) nobu.nokada@softhome.net wrote: > Hi, > > At Wed, 26 May 2004 08:13:25 +0900, > Joel VanderWerf wrote in [ruby-talk:101419]: > >>>You don't need save $SAFE, which is always thread local. And, >>>Proc also preserves its $SAFE value. Though this was an >>>unofficial feature in 1.6, guaranteed from 1.8. >> >>Hm. Didn't know that. Also, methods preserve $SAFE: >> >>def foo; $SAFE=3; end >># => nil >>$SAFE >># => 0 >>foo >># => 3 >>$SAFE >># => 0 > > > Do you mean Method object? > > $ ruby19 -ve 'def foo;$SAFE=3;$SAFE;end;p method(:foo).call;p $SAFE' > ruby 1.9.0 (2004-05-25) [i686-linux] > 3 > 0 > > $ ruby19 -ve 'def foo;$SAFE=3;$SAFE;end;p foo;p $SAFE' > ruby 1.9.0 (2004-05-25) [i686-linux] > 3 > 3 > > $ ruby18 -ve 'def foo;$SAFE=3;$SAFE;end;p foo;p $SAFE' > ruby 1.8.2 (2004-05-24) [i686-linux] > 3 > 3 > I was confused by irb: $ irb irb(main):001:0> def foo; $SAFE=3; end; foo; $SAFE => 3 irb(main):002:0> $SAFE => 0 irb(main):003:0> $SAFE = 3 => 3 irb(main):002:0> $SAFE => 0 So it is really irb that is saving and restoring $SAFE around each input line. Maybe this is because irb is wrapping the input into a Proc.