Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > and in ternary operator

Reply
Thread Tools

and in ternary operator

 
 
Parv G.
Guest
Posts: n/a
 
      06-04-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      06-04-2009
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
>
> Thank you.


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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
 
 
 
Pieter V.
Guest
Posts: n/a
 
      06-04-2009
On Thu, Jun 4, 2009 at 12:13 PM, Parv G. <(E-Mail Removed)> wrote:
> 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

>
> Thank you.
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Rolando Abarca
Guest
Posts: n/a
 
      06-04-2009
On Jun 4, 2009, at 3:13 PM, 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


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

do_a
do_b if true

>

regards,
--
Rolando Abarca M.





 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      06-04-2009
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)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      06-04-2009
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.

--
Nobu Nakada

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      06-05-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ternary operator and tuple unpacking -- What am I missing ? imageguy Python 7 01-13-2009 09:58 PM
Ternary operator and memory access Udo A. Steinberg C Programming 20 08-08-2006 12:57 AM
generator expressions and new ternary operator zipher Python 2 09-13-2004 07:02 AM
ternary operator and ostreams Roger Leigh C++ 6 01-19-2004 07:02 PM
union, ternary operator, and C. What a mess! Paul E Johnson C Programming 3 10-17-2003 03:44 AM



Advertisments