J
Jarmo Pertman
Hello!
I've just landed upon a very interesting landmine by using defined?
Consider the following code:
# a.rb
module A
autoload :B, "b.rb"
end
defined?(A::B::C)
p A::B
p A::B.new.one
p A::B.new.two
# b.rb
module A
class B
puts "loading"
def one; 1; end
raise "no-no-no!"
def two; 2; end
end
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:
loading
A::B
1
a.rb:8: undefined method `two' for #<A::B:0x49590c0> (NoMethodError)
This happens with 1.8.6 and 1.8.7, but is working as expected with
1.9.1.
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...
Jarmo Pertman
I've just landed upon a very interesting landmine by using defined?
Consider the following code:
# a.rb
module A
autoload :B, "b.rb"
end
defined?(A::B::C)
p A::B
p A::B.new.one
p A::B.new.two
# b.rb
module A
class B
puts "loading"
def one; 1; end
raise "no-no-no!"
def two; 2; end
end
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:
loading
A::B
1
a.rb:8: undefined method `two' for #<A::B:0x49590c0> (NoMethodError)
This happens with 1.8.6 and 1.8.7, but is working as expected with
1.9.1.
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...
Jarmo Pertman