From: Stefano Crocco Date: 2009-12-22T22:53:29+09:00 Subject: Re: newbie: if is not null else... On Tuesday 22 December 2009, 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. > | > |Thank you very much, > |Al I don't know PHP, so I'm not sure whether this does exactly what you want: $x ||= '-' This is a short form for $x = $x || '-' The right hand side of this expression returns $x if it exists and isn't nil or false and '-' otherwise. Note that this won't work if $x can contain the value false. In this case, you'll have to use a longer expression: $x = '-' if $x.nil? However, the above line will produces a warning if $x hasn't been used before. If you don't want this, you need something longer still: $x = '-' if (!defined? $x or $x.nil?) I hope this helps Stefano