On Wed, Sep 22, 2010 at 5:35 PM, Jarmo Pertman <jarmo.p / gmail.com> wrote:
> Hello!
>
> I've just landed upon a very interesting landmine by using defined?
>
> Consider the following code:
>
> # a.rb
> module A
> =A0autoload :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
> =A0 =A0class B
> =A0 =A0 =A0 =A0puts "loading"
> =A0 =A0 =A0 =A0def one; 1; end
>
> =A0 =A0 =A0 =A0raise "no-no-no!"
>
> =A0 =A0 =A0 =A0def two; 2; end
> =A0 =A0end
> end
>
> So, when running a.rb what would you expect?
>
> I would expect defined? line to raise an RuntimeError,...

Why?

defined? is there to check WITHOUT raising an runtime error

ruby-1.8.7-p302 > defined? Blatz
 =3D> nil
ruby-1.8.7-p302 > defined? A::B::C
 =3D> nil
ruby-1.8.7-p302 > defined? Object
 =3D> "constant"

And it's the same for 1.9.2

ruby-1.9.2-p0 > defined? Blatz
 =3D> nil
ruby-1.9.2-p0 > defined? A::B::C
 =3D> nil
ruby-1.9.2-p0 > defined? Object
 =3D> "constant"



--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale