and in ternary operator

Discussion in 'Ruby' started by Parv G., Jun 4, 2009.

  1. Parv G.

    Parv G. Guest

    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/.
    Parv G., Jun 4, 2009
    #1
    1. Advertising

  2. 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
    Joel VanderWerf, Jun 4, 2009
    #2
    1. Advertising

  3. Parv G.

    Pieter V. Guest

    On Thu, Jun 4, 2009 at 12:13 PM, Parv G. <> 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/.
    >
    >
    Pieter V., Jun 4, 2009
    #3
  4. 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.
    Rolando Abarca, Jun 4, 2009
    #4
  5. 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/.
    Brian Candler, Jun 4, 2009
    #5
  6. 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
    Nobuyoshi Nakada, Jun 5, 2009
    #6
  7. 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/.
    Brian Candler, Jun 5, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    658
    Roger Leigh
    Jan 19, 2004
  2. Paul E Johnson

    union, ternary operator, and C. What a mess!

    Paul E Johnson, Oct 17, 2003, in forum: C Programming
    Replies:
    3
    Views:
    460
    Ed Morton
    Oct 17, 2003
  3. zipher
    Replies:
    2
    Views:
    355
    Gerrit
    Sep 13, 2004
  4. Udo A. Steinberg

    Ternary operator and memory access

    Udo A. Steinberg, Aug 7, 2006, in forum: C Programming
    Replies:
    20
    Views:
    558
    Keith Thompson
    Aug 8, 2006
  5. imageguy
    Replies:
    7
    Views:
    293
    John Machin
    Jan 13, 2009
Loading...

Share This Page