From: "William James" <> Date: 2009-12-23T04:00:09+09:00 Subject: Re: newbie: if is not null else... Alfonso Caponi wrote: > Rick Denatale wrote: > > On Tue, Dec 22, 2009 at 8:32 AM, Alfonso Caponi > > wrote: > >> Hi forum, > > > > >> how can I replace this PHP syntax with Ruby? > > > > >> $x = (isset($x) and !empty($x)) ? $x : '-'; > > > > >> I would like assign a default value if $x is Null. > > > > The approximate Ruby idiom would be > > > > $x ||= '-' > > > > The only caveat is that this would (re)assign the value if $x were > > either nil or false. This is usually not an issue, but if so then > > you could use > > > > $x = '-' if $x.nil? > > > > Thank you very much Rick. Using: > > x = '-' if x.nil? > > I've x = Null again.. :( > > It seems works with > > if (x.to_s =~ Null) > x = '-' > end > > but I don't think is the best way to write it :( What is Null? --