On 2009-12-22, Alfonso Caponi <alfonso.caponi / gmail.com> wrote:
> how can I replace this PHP syntax with Ruby?

It's a bit tricky.

> $x = (isset($x) and !empty($x)) ? $x : '-';

> I would like assign a default value if $x is Null.

Well, not quite.  See, you're checking two things; one is whether it's
set or not, the other is whether it has a value.

Here's the big shocker for you:  In Ruby, '' is a true value, because
it's not nil and it's not false.

The normal Ruby idiom is:

	x || '-'

which means "$x if it's set and not false, otherwise '-'", but that isn't
what you want.  (In PHP, that'd be 1, because || returns 0 or 1... This bit
me recently.)

But that doesn't help you in the case where you want to replace empty strings
with '-'.  For that, maybe something like:

	x = (x && !x.empty?) ? x : '-'

(That assumes x has an empty? predicate, not everything does.)

(BTW, you'll also get burned by the fact that 0 is true in ruby, because
it's neither false nor nil.)

-s
-- 
Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nospam / seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!