Forum: Ruby conditional statements within test method parameters?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee6788c2faaad7a5cd0bc466c9ea60d8?d=identicon&s=25 unknown (Guest)
on 2007-04-13 16:45
(Received via mailing list)
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
376b37712da46bc511eac502afe86422?d=identicon&s=25 Reuben Grinberg (Guest)
on 2007-04-13 17:20
(Received via mailing list)
aidy.lewis@googlemail.com 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-04-13 18:12
(Received via mailing list)
On 13.04.2007 16:40, aidy.lewis@googlemail.com wrote:
> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-04-13 21:02
(Received via mailing list)
On Sat, Apr 14, 2007 at 12:20:06AM +0900, Reuben Grinberg 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
This topic is locked and can not be replied to.