Require resets $! to nil. Bug or feature?

batsman@tux-chan:/tmp$ cat excep2.rb

begin
raise "foo"
rescue Exception
puts "$! is #{$!.inspect}"
require 'digest/md5’
puts "$! is #{$!.inspect}"
end
batsman@tux-chan:/tmp$ ruby excep2.rb
$! is #<RuntimeError: foo>
$! is nil

I was bitten by this when doing something like

def somemeth
require ‘digest/md5’

end

def foo

begin

rescue Exception
somemeth
raise # I expected this to re-raise the exception, not to
# create a new one with an empty message
end

end

Note that
rescue Exception => e
somemeth
raise e
end
is not a desirable solution because the backtrace info will be modified.
For now I will move the requires but I’d like to know if require’s
behaviour w.r.t. $! is deliberate, and if so, the rationale.

I’ve scanned the first ~ 80000 messages of ruby-talk and couldn’t find
any reference to this issue.

···


Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux
over the wire”. Film at 11.’
– Linus Torvalds

Hi,

It’s not defined behavior. I just couldn’t think of any use of $!
value after require.

···

In message “require resets $! to nil. Bug or feature?” on 04/03/20, Mauricio Fernández batsman.geo@yahoo.com writes:

Note that
rescue Exception => e
somemeth
raise e
end
is not a desirable solution because the backtrace info will be modified.
For now I will move the requires but I’d like to know if require’s
behaviour w.r.t. $! is deliberate, and if so, the rationale.

I feel like backtrace information should be preserved this case.
There might be some reason behind that I forget. Let me check.

						matz.