and in ternary operator

P

Parv G.

hi,

i would like to do something like the following

if true
do_a
else
d_a
d_b
end

Can this be done using ternary operator?

This seem to give different result:
ifTrue ? do_a : do_a and do_b

Thank you.
 
J

Joel VanderWerf

Parv said:
hi,

i would like to do something like the following

if true
do_a
else
d_a
d_b
end

Can this be done using ternary operator?

This seem to give different result:
ifTrue ? do_a : do_a and do_b

Thank you.

result = false ? nil : (x=1; x+=10; x)
p result # ==> 11
 
P

Pieter V.

hi,

i would like to do something like the following

if true
=C2=A0do_a
else
=C2=A0d_a
=C2=A0d_b
end

Can this be done using ternary operator?

This seem to give different result:
ifTrue ? do_a : do_a and do_b

If the goal is to always run the truthy condition and optionally some
extra code for the falsey one, this will work as well:

do_a ; do_b if ifTrue
 
R

Rolando Abarca

hi,

i would like to do something like the following

if true
do_a
else
d_a
d_b
end

Can this be done using ternary operator?

This seem to give different result:
ifTrue ? do_a : do_a and do_b

since you always "do_a", why no execute it always?

do_a
do_b if true
regards,
 
B

Brian Candler

Parv said:
hi,

i would like to do something like the following

if true
do_a
else
d_a
d_b
end

Can this be done using ternary operator?

This seem to give different result:
ifTrue ? do_a : do_a and do_b

'and' is very low precendence operator, and also will only do_b if the
result of do_a is true.

ifTrue ? do_a : (do_a, do_b)
 
N

Nobuyoshi Nakada

Hi,

At Fri, 5 Jun 2009 05:54:48 +0900,
Brian Candler wrote in [ruby-talk:338415]:
ifTrue ? do_a : (do_a, do_b)

You need a semicolon instead of a comma.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top