From: Rick DeNatale Date: 2010-09-23T08:06:33+09:00 Subject: Re: Unexpected behaviour of defined? under 1.8.x On Wed, Sep 22, 2010 at 5:35 PM, Jarmo Pertman wrote: > 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,... Why? defined? is there to check WITHOUT raising an runtime error ruby-1.8.7-p302 > defined? Blatz => nil ruby-1.8.7-p302 > defined? A::B::C => nil ruby-1.8.7-p302 > defined? Object => "constant" And it's the same for 1.9.2 ruby-1.9.2-p0 > defined? Blatz => nil ruby-1.9.2-p0 > defined? A::B::C => nil ruby-1.9.2-p0 > defined? Object => "constant" -- 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