# Conditional statements within test method parameters?

**URL:** <https://www.ruby-forum.com/t/conditional-statements-within-test-method-parameters/91937>\
**Category:** Ruby\
**Created:** [April 13, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/conditional-statements-within-test-method-parameters/91937 "2007-04-13T16:45:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 13, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/conditional-statements-within-test-method-parameters/91937/1 "2007-04-13T16:45:43Z")

</div>

hi,

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

test\_results((assert(REMEMBER\_ME.isSet?)), \*msg)

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

Something like:

test\_results((assert(REMEMBER\_ME.isSet?)), unless 'the object is not set') ?

cheers

aidy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 13, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/conditional-statements-within-test-method-parameters/91937/2 "2007-04-13T17:20:45Z")

</div>

removed\_email\_address@domain.invalid wrote:

> parameters  
> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 13, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/conditional-statements-within-test-method-parameters/91937/3 "2007-04-13T18:12:52Z")

</div>

On 13.04.2007 16:40, removed\_email\_address@domain.invalid wrote:

> parameters
> 
> Something like:
> 
> test\_results((assert(REMEMBER\_ME.isSet?)), unless 'the object is not set') ?
> 
> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 13, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/conditional-statements-within-test-method-parameters/91937/4 "2007-04-13T21:02:09Z")

</div>

On Sat, Apr 14, 2007 at 12:20:06AM +0900, Reuben G. wrote:

> > Something like:
> 
> 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
