conditional statements within test method parameters?

A

aidy.lewis

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
 
R

Reuben Grinberg

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
 
R

Robert Klemme

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
 
B

Brian Candler

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,240
Messages
2,571,208
Members
47,845
Latest member
vojosay

Latest Threads

Top