I’ve just landed upon a very interesting landmine by using defined?
Consider the following code:
autoload :B, “b.rb”
def one; 1; end
def two; 2; end
So, when running a.rb what would you expect?
I would expect defined? line to raise an RuntimeError, but instead i
will see this:
a.rb:8: undefined method `two’ for #<A:0x49590c0> (NoMethodError)
This happens with 1.8.6 and 1.8.7, but is working as expected with
In real life the code was something like this:
return unless defined?(A::B.some_method)
During the autoload, there was a ton of require statements of which
one failed with LoadError, but this didn’t get raised to the console
and half of the unit-tests were failing. Yes, you guessed it right - i
did spend some amount of time with the debugger
Anyway, i’ve fixed that line to use respond_to? instead, but it is
still odd behaviour…
I would expect defined? line to raise an RuntimeError,…
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::C”, then of course A::B has to be autoloaded before you check
A::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 ----
autoload :B, “b.rb”
p defined?(A::C) # nil
p defined?(A::C) # “constant”
---- b.rb ----
C = 123
puts “Never get here”
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:
b.rb:6:in <class:B>': no-no-no! (RuntimeError) from b.rb:2:inmodule:A’
from b.rb:1:in <top (required)>' 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?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.