From: Brian Candler Date: 2004-10-05T17:40:34+09:00 Subject: Re: Using Sqlite for processing On Tue, Oct 05, 2004 at 05:14:52AM +0900, Ara.T.Howard@noaa.gov wrote: > yn Tue, 5 Oct 2004, Brian Candler wrote: > > >Just a minor point, but db is *always* defined at that point in the code, > >and so this test is useless. It's defined even if an exception is raised > >*before* the statement which assigns to db (see example below). > > it may be defined, but it may not respond to 'close' if Database::new blew > up. > eg: > > > harp:~ > cat a.rb > begin > db = raise > ensure > db.close if defined? db > end > > harp:~ > ruby a.rb > a.rb:5: undefined method `close' for nil:NilClass (NoMethodError) That's exactly what I was saying. The clause "if defined? db" should not be there, because it always evaluates to a true value. > db.close if db and db.respond_to 'close' That assumes you don't know whether db was created as a database object or something completely different! Which might be the case in some programs, but I think in most cases you'd know that db was either nil or something which responded to 'close'. However, calling 'close' might raise an exception anyway (e.g. because the database is already closed) Regards, Brian.