And in ternary operator


#1

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.


#2

Parv G. wrote:

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


#3

On Thu, Jun 4, 2009 at 12:13 PM, Parv G. removed_email_address@domain.invalid wrote:

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


#4

Parv G. wrote:

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)


#5

Hi,

At Fri, 5 Jun 2009 05:54:48 +0900,
Brian C. wrote in [ruby-talk:338415]:

ifTrue ? do_a : (do_a, do_b)

You need a semicolon instead of a comma.


#6

Nobuyoshi N. wrote:

Brian C. wrote in [ruby-talk:338415]:

ifTrue ? do_a : (do_a, do_b)

You need a semicolon instead of a comma.

Sorry, that’s me with my C head on :slight_smile:


#7

On Jun 4, 2009, at 3:13 PM, Parv G. wrote:

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,