Need help regarding conditionals on while loops

In a tutorial I’m currently reading
(, this came up.

def doUntilFalse firstInput, someProc
input = firstInput
output = firstInput

while output
input = output
output = input


They never really explained how this works though:

while output

What exactly is Ruby checking for here? The author uses this code for
the method:

puts doUntilFalse([5], buildArrayOfSquares).inspect

So the while loop would be something like this: “while [5]”
Shouldn’t it be like “while [5] != something”?

I’m extremely confused about this :), as far as I can see, it’s like
asking ‘What’s the difference between an elephant.’ If someone could
just simply explain this it would be awesome! Thanks in advance!

On Sep 13, 2011, at 10:49 PM, Kane W. wrote:

They never really explained how this works though:

while output

The while loop body is executed if the expression evaluates to a
true-ish value.
In this case the expression is simply the local variable ‘output’.

In Ruby all values are considered true except for nil and false. In
0 is considered true as is an empty string, empty array, and empty hash
("", [], {}).
This often trips up Ruby newcomers.

So in your example

the while loop will continue iterating until someProc returns a
false-ish value
(either nil or false). The output of someProc is reused as its input for
next iteration.

Gary W.

Thanks that was very helpful!