From: Robert Klemme Date: 2004-07-05T16:37:41+09:00 Subject: Re: How to detect if variable has been defined yet? "Randy Lawrence" schrieb im Newsbeitrag news:JJhFc.31726$eH1.15066611@newssvr28.news.prodigy.com... > Sean O'Dell wrote: > > > On Friday 02 July 2004 10:49, Randy Lawrence wrote: > > > >>Hi, > >> > >>I'd like to detect if a variable has been defined yet. Is this possible > >>in Ruby or mod_ruby? > >> > >>For example, I'd like to do the following: > >> > >>if $total.exists > >> $total = $total + 1 > >>else > >> $total = 0 > >>end > > > > > > if defined?($total) > > $total = $total + 1 > > else > > $total = 0 > > end > > > > > > Sean O'Dell > > > > > > Thanks! This was exactly what was needed and it works. Just an additional note: from runtime performance it is much more efficient to first initialize a variable and then use it. The code looks cleaner then, too. $total = 0 .... some loop $total += 1 end Regards robert