conditional statements within test method parameters?

Discussion in 'Ruby' started by aidy.lewis@googlemail.com, Apr 13, 2007.

  1. Guest

    hi,

    I have a test method that writes some xml. The second parameter is
    optional, the first parameter is boolean.

    <snip>
    test_results((assert(REMEMBER_ME.isSet?)), *msg)
    </snip>

    Would it be possible to embed a conditional statement between these
    parameters

    Something like:

    <snip>
    test_results((assert(REMEMBER_ME.isSet?)), unless 'the object is not
    set') ?
    </snip>

    cheers

    aidy
     
    , Apr 13, 2007
    #1
    1. Advertising

  2. wrote:
    > hi,
    >
    > I have a test method that writes some xml. The second parameter is
    > optional, the first parameter is boolean.
    >
    > <snip>
    > test_results((assert(REMEMBER_ME.isSet?)), *msg)
    > </snip>
    >
    > Would it be possible to embed a conditional statement between these
    > parameters
    >
    > Something like:
    >
    > <snip>
    > test_results((assert(REMEMBER_ME.isSet?)), unless 'the object is not
    > set') ?
    > </snip>
    >
    > cheers
    >
    > aidy
    >


    Yes, but it's not necessarily pretty:

    def test(one, *two)
    puts one
    two.each { |t| puts t}
    end

    irb> test("bob", "jack", "andy")
    bob
    jack
    andy
    irb> test("bob")
    bob
    irb> some_condition = false
    irb> test("bob", some_condition ? ["jack", "andy"] : [])
    bob
    irb>

    The "optional" argument is actually a list, so passing in the empty list
    for it is the same thing as not passing it in.

    Cheers,
    Reuben
     
    Reuben Grinberg, Apr 13, 2007
    #2
    1. Advertising

  3. On 13.04.2007 16:40, wrote:
    > hi,
    >
    > I have a test method that writes some xml. The second parameter is
    > optional, the first parameter is boolean.
    >
    > <snip>
    > test_results((assert(REMEMBER_ME.isSet?)), *msg)
    > </snip>
    >
    > Would it be possible to embed a conditional statement between these
    > parameters
    >
    > Something like:
    >
    > <snip>
    > test_results((assert(REMEMBER_ME.isSet?)), unless 'the object is not
    > set') ?
    > </snip>
    >
    > cheers


    I'm not sure what you are trying to achieve. But it seems strange to
    have an assert and caveat with an "unless". Can you explain a bit more
    what you want to do?

    Kind regards

    robert
     
    Robert Klemme, Apr 13, 2007
    #3
  4. On Sat, Apr 14, 2007 at 12:20:06AM +0900, Reuben Grinberg wrote:
    > >I have a test method that writes some xml. The second parameter is
    > >optional, the first parameter is boolean.
    > >
    > ><snip>
    > >test_results((assert(REMEMBER_ME.isSet?)), *msg)
    > ></snip>
    > >
    > >Would it be possible to embed a conditional statement between these
    > >parameters
    > >
    > >Something like:
    > >
    > ><snip>
    > >test_results((assert(REMEMBER_ME.isSet?)), unless 'the object is not
    > >set') ?
    > ></snip>
    > >
    > >cheers
    > >
    > >aidy
    > >

    >
    > Yes, but it's not necessarily pretty:
    >
    > def test(one, *two)
    > puts one
    > two.each { |t| puts t}
    > end
    >
    > irb> test("bob", "jack", "andy")
    > bob
    > jack
    > andy
    > irb> test("bob")
    > bob
    > irb> some_condition = false
    > irb> test("bob", some_condition ? ["jack", "andy"] : [])
    > bob
    > irb>
    >
    > The "optional" argument is actually a list, so passing in the empty list
    > for it is the same thing as not passing it in.


    I think you missed a '*' on the method call there.

    As you've written it, in each case you're passing exactly two arguments, the
    second of which is either ["jack","andy"] or [], so the array 'two' will
    consist of either [["jack","andy"]] or [[]]

    def test(one, *two)
    puts one.inspect, two.inspect
    puts
    end

    test("bob","jack","andy")
    test("bob")
    [false,true].each do |some_condition|
    test("bob", some_condition ? ["jack", "andy"] : [])
    end
    # This is what it should have been
    [false,true].each do |some_condition|
    test("bob", *(some_condition ? ["jack", "andy"] : []))
    end
     
    Brian Candler, Apr 13, 2007
    #4
    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. Neil Zanella
    Replies:
    8
    Views:
    1,233
    mfmehdi
    Oct 20, 2006
  2. Cowboy Bob
    Replies:
    1
    Views:
    3,446
    Matt Parker
    Jan 29, 2004
  3. Harry George
    Replies:
    6
    Views:
    444
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    776
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    290
    John Crichton
    Jul 12, 2010
Loading...

Share This Page