Newbie: if is not null else

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

On Tue, Dec 22, 2009 at 8:32 AM, Alfonso C.
[email protected] 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?


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Rick Denatale wrote:

On Tue, Dec 22, 2009 at 8:32 AM, Alfonso C.
[email protected] 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… :frowning:

It seems works with

if (x.to_s =~ /Null/)
x = ‘-’
end

but I don’t think is the best way to write it :frowning:

ciao
waiting for a confirmation from the forum guys…

since nil is false in Ruby

x = a_value if x.nil?
equals
x = a_value if x == false
and also
x = a_value unless x
or
x ||= a_value

On Tuesday 22 December 2009, Alfonso C. 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

my post was unaware of the preceding replies

you have
x.to_s =~ /Null/

what is x?
try
p x.class

On 2009-12-22, Alfonso C. [email protected] 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

On Dec 22, 2009, at 5:55 AM, Giampiero Z. wrote:

or
x ||= a_value

Not exactly…

x = nil
=> nil

x.nil?
=> true

x == false
=> false

puts “Hi!” unless x
Hi!
=> nil

x = false
=> false

x.nil?
=> false

x == false
=> true

puts “Hi!” unless x
Hi!
=> nil

false and nil both evaluate to false in conditionals, everything else
(including 0) evaluates to true. However, “x == false” is asking if x is
exactly false. For fun, try “p false.class”…

Cheers,

Josh

On Tue, Dec 22, 2009 at 7:53 AM, Alfonso C.
[email protected]wrote:

Thank you very much Rick. Using:

x = ‘-’ if x.nil?

I’ve x = Null again… :frowning:

x should not be Null at this point. First, Null doesn’t exist, in Ruby
it is
nil. Second, if you are using nil, then this should do what you want

x = nil
x = ‘-’ if x.nil?
x # => “-”

x = ‘abc’
x = ‘-’ if x.nil?
x # => “abc”

It seems works with

if (x.to_s =~ /Null/)
x = ‘-’
end

but I don’t think is the best way to write it :frowning:

That probably isn’t doing what you think / want. Since Null should be
nil,
the only way that should match is if your class somehow returns a string
with ‘Null’ in it. That seems pretty unlikely.

I think Rick’s answer is probably what you are looking for, but if an
empty
string evaluates to false in php, then you will need to add the
condition
for that, ie

x = ‘-’ if !x || x.empty?

This assumes that x is expected to be a string, or nil.

You should look at your code and make switch out your ‘Null’ with ‘nil’
,
and if this hasn’t solved your issue, perhaps explain what you are
trying to
do, ie give a series of different inputs, and what you want it to do to
them.

On Dec 22, 6:53 am, Alfonso C. [email protected] wrote:

end
Null is not a special value in Ruby. I am guessing that perhaps you
have a string with the value “Null” (four characters, starting with
capital N; not a special value).

Try:
p x, x.class
or
puts x.inspect, x.class
to gain insight about what value you actually have there.

On Dec 22, 2009, at 5:53 AM, Stefano C. wrote:

|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 || ‘-’

Correction, it is short form for:

$x || $x = ‘-’

…trust me, we’ve been over this at least 3 times, at great length, on
the mailing list. :wink:

Cheers,

Josh

On Wednesday 23 December 2009, Joshua B. wrote:

|Correction, it is short form for:
|
|$x || $x = ‘-’
|
|…trust me, we’ve been over this at least 3 times, at great length, on
|the mailing list. :wink:

I may be missing something, but according to page 96 of “The Ruby
Programming
Language”, my version seems to be the correct one. It states that:

results ||= []

expands to

results = results || []

Stefano

2009/12/23 Stefano C. [email protected]:

|
results ||= []

expands to

results = results || []

For a simple assignment the difference is negligible but it matters if
you do

h = {}
h[1] ||= 2

Please search the archives, Josh is right. Btw, you can easily prove
it by doing

irb(main):001:0> x = Class.new { def a;puts “a”;@a;end; def a=(x);puts
“a=”;@a=x;end }.new
=> #<#Class:0x10135234:0x10134d10>
irb(main):002:0> x.a ||= 123
a
a=
=> 123
irb(main):003:0> x.a ||= 456
a
=> 123
irb(main):004:0> x.a = x.a || 789
a
a=
=> 123
irb(main):005:0>

If you were right the output of line 3 would look like the one of line
4.

Kind regards

robert

On Dec 22, 2009, at 5:30 PM, Seebs wrote:

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.)

I’ve always found the easiest way to replace empty strings is:

x = ‘-’ if x.to_s.empty?

Just about everything has a #to_s method, and nil evaluates to an empty
string, so this will replace x if it is either an empty string or if
it is nil or if x is currently not yet defined.

Cheers,

Josh

If you’re using Rails (or more specifically the ActiveSupport library),
then you can do

x = ‘-’ if x.blank?

I thought it was worth mentioning in case the OP is moving from PHP to
Rails.

BTW you almost certainly won’t want $x (a global variable) in Ruby.