From: Kristof Bastiaensen Date: 2004-09-18T21:49:55+09:00 Subject: Re: [Nuby Question] Return value of while loop. On Sat, 18 Sep 2004 10:43:18 +0200, Brian Schr�der wrote: > Maybe this question would be more adequate for the ruby-nuby forum ;), but > while I'm writing my lecture slides about ruby, I saw that a while loop > always returns nil. Eg. > > i = 1 # => 0 > i *= 2 while i > 100 # => nil > > I would have expected the loop to return 128. Is there an intention behind > this? Just thought I'd voice my "least surprise" to see if for others this > is also against the POTS. > Here is my guess: I would suspect while to return the last statement. The last statement will always be the test from the while, and always false. In this case it may be more logical to return nil. However it may be more useful if while returned the last value of the block. > PS: If you want you can see this as a warmup for our first Quiz ;). What > is the most concise way to write this: > > i = 1 > loop do > i *= 2 > break i if i > 100 > end > > which would simulate the behaviour I'd expect of the 2-liner above. (No > semicolons allowed) Here is a oneliner: i = 128 Regards, KB