newbie: if is not null else...

A

Alfonso Caponi

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
 
R

Rick DeNatale

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
 
A

Alfonso Caponi

Rick said:
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 :(
 
S

Stefano Crocco

|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
 
G

Giampiero Zanchi

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
 
G

Giampiero Zanchi

my post was unaware of the preceding replies

you have
x.to_s =~ /Null/

what is x?
try
p x.class
 
W

William James

Alfonso said:
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?

--
 
S

Seebs

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
 
P

Phrogz

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

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

Joshua Ballanco

=20
I don't know PHP, so I'm not sure whether this does exactly what you = want:
=20
$x ||=3D '-'
=20
This is a short form for=20
=20
$x =3D $x || '-'

Correction, it is short form for:

$x || $x =3D '-'

...trust me, we've been over this at least 3 times, at great length, on =
the mailing list. ;-)

Cheers,

Josh=
 
J

Joshua Ballanco

ciao
waiting for a confirmation from the forum guys...
=20
since nil is false in Ruby
=20
x =3D a_value if x.nil?
equals
x =3D a_value if x =3D=3D false
and also
x =3D a_value unless x
or
x ||=3D a_value

Not exactly...
Hi!
=3D> nilHi!
=3D> nil

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

Cheers,

Josh=
 
J

Joshua Ballanco

But that doesn't help you in the case where you want to replace empty = strings
with '-'. For that, maybe something like:
=20
x =3D (x && !x.empty?) ? x : '-'
=20
(That assumes x has an empty? predicate, not everything does.)

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

x =3D '-' 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=
 
S

Stefano Crocco

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

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
 
R

Robert Klemme

2009/12/23 Stefano Crocco said:
|> $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. ;-)

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 || []

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Thank you very much Rick. Using:

x = '-' if x.nil?

I've x = Null again.. :(

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 :(

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

Brian Candler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top