From: "Ara.T.Howard" Date: 2004-07-30T23:51:46+09:00 Subject: Re: defined? in ruby-1.9 On Fri, 30 Jul 2004, Mark Sparshatt wrote: > Ara.T.Howard wrote: > >> On Fri, 30 Jul 2004, Yukihiro Matsumoto wrote: >> >>> Hi, >>> >>> In message "defined? in ruby-1.9" >>> on 04/07/30, "Dmitry V. Sabanin" writes: >>> >>> |Lately I was converting some of my projects to ruby 1.9, and I found >>> that this code is no longer working: >>> | raise if defined?(Test::Unit) >>> |Throws: >>> | uninitialized constant Test (NameError) >>> | >>> |Ruby 1.8 silently works with that code. So, how do I need to handle >>> this kind of checks in 1.9? >>> |For now as a workaround I changed Test::Unit to Test and it works >>> fine. >>> >>> Because 1.8 silently ignores NameError in the check for Test::Unit. >>> >>> matz. >> >> >> but isn't that what 'defined?' is for? to determine if the compiler >> has seen >> a 'name' yet? >> > I supposed the rational is that by running > > defined?(Test::Unit) > > You are checking if the Test class/module contains a constant Unit. If > Test doesn't exist then Ruby can't check this, so it throws a NameError. > > The obvious, though verbose, workaround would be to do > > defined?(Test) && defined(Test::Unit) > > -- > Mark Sparshatt ah - hadn't thought of that. that it better than failing silently. cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================