[Nuby Question] Return value of while loop

That was a typo, should have been

I'm sorry. I was looking at your code, not on your question.

But I think "while" is just a modifier not an expression nor method.

You mention about returning something. Consider this:

j = 1
k = 1

k *= 2 while j < 100

what would the whole statement return: k or j?

(sorry I'm using variable k instead of I since outlook is overriding :slight_smile:

cheers,

Brian

kind regards -botp

···

Brian Schröder [mailto:ruby@brian-schroeder.de] wrote:

I'd expect it to return the last statement in the while block. That would
be k = k * 2. (But the examples is an infinite loop, so it would return
nothing).

Maybe it is clearer if I write
while j < 100
  j *= 2
end.

As someone else pointed out, the last thing that was evaluated is the
comparision, but I expected that always the last result from the block was
returned. But if you look at it strictly it's not that clear what is the
last statement.

On the other hand, now we get back nothing, and what you get from the
comparision is always false, so returning the last thing of the block
would give us more information.

regards,

Brian

···

Am Sat, 18 Sep 2004 18:57:23 +0900 schrieb Peña, Botp:

Brian Schröder [mailto:ruby@brian-schroeder.de] wrote:

That was a typo, should have been

I'm sorry. I was looking at your code, not on your question.

But I think "while" is just a modifier not an expression nor method.

You mention about returning something. Consider this:

j = 1
k = 1

k *= 2 while j < 100

what would the whole statement return: k or j?