From: Jarmo Pertman Date: 2010-09-24T05:35:10+09:00 Subject: Re: Unexpected behaviour of defined? under 1.8.x I'm fully aware that it was related with autoload and why the file got partially loaded, but it was just strange behaviour. I didn't expect that defined? rescues silently any Exceptions (or is there anything which isn't rescued?). Considering from the name "defined?" i thought that it will rescue only NameError (which could happen only if trying to see if any const/class/module/variable is not defined), but rescueing everything didn't make sense. As i pointed out then in 1.9.1 the result is more of an expected: loading b.rb:6:in `': no-no-no! (RuntimeError) from b.rb:2:in `' from b.rb:1:in `' from a.rb:6:in `
' Just checked that 1.9.2 works the same. Rick, why do you expect it to rescue every Exception? What's your logic behind that assumption or is it just because you knew that this is the correct behavior? Jarmo On Sep 23, 11:17 am, Brian Candler wrote: > Rick Denatale wrote: > >> I would expect defined? line to raise an RuntimeError,... > > > Why? > > > defined? is there to check WITHOUT raising an runtime error > > I think the point is the autoload stuff. > > If you have autoload set up for A::B, and you are checking "defined? > A::B::C", then of course A::B has to be autoloaded before you check > A::B::C. But when an exception is raised in the source file which is > being autoloaded, it just finishes at that point silently, so you end up > with half a file loaded. > > Here is another example: > > ---- a.rb ---- > module A >   autoload :B, "b.rb" > end > > p defined?(A::B::C)   # nil > p defined?(A::B::C)   # "constant" > > ---- b.rb ---- > module A >   class B >     puts "Starting" >     C = 123 >     raise "Oops" >     puts "Never get here" >   end > end > -- > Posted viahttp://www.ruby-forum.com/.