Forum: Ruby and in ternary operator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
520d0b178a769fdffe5cd42b7132cdcc?d=identicon&s=25 Parv G. (parvg)
on 2009-06-04 21:13
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.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-06-04 21:20
(Received via mailing list)
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
Eb658fffacf97d23cf7df98003d38017?d=identicon&s=25 Pieter V. (Guest)
on 2009-06-04 21:23
(Received via mailing list)
On Thu, Jun 4, 2009 at 12:13 PM, Parv G. <ghotrapa@yahoo.com> 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
79caabff3b239b1bee6de8c51bc7f78c?d=identicon&s=25 Rolando Abarca (Guest)
on 2009-06-04 21:30
(Received via mailing list)
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,
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-04 22:54
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)
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2009-06-05 01:42
(Received via mailing list)
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-05 14:23
Nobuyoshi Nakada wrote:
> Brian Candler 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 :-)
This topic is locked and can not be replied to.