Hi --

On Fri, 30 Apr 2010, Michel Demazure wrote:

> Robert Klemme wrote:
>> 2010/4/30 Michel Demazure <michel / demazure.com>:
>>>
>>> irb(main):010:0> x += 1 while x < 15
>>> => nil
>>>
>>> It would be nice to get the return value of the last instruction
>>> *before* the test fails.
>>
>> Why?  What use case is there that would require this or what code
>> would become (much) more elegant?
>>
> I was just cleaning a library and found two methods ending with
> something like
>
>  ind += 1 while <some_test>
>  ind
> end
>
> and tried to get rid of the last line, quite ugly and looking
> unnecessary. You're right : not *much* more elegant. But once you get
> used to syntactic sugar, you'll ask for some more ... Remember Dijsktra
> : "abuse of syntactic sugar gives cancer of the semi-colon".

I don't think there's any syntactic sugar here. I always think of
syntactic sugar as "It looks like it's doing X, but it's really [or
also] doing Y." For example, x * y, where it looks like an infix
operator but is really sending a message to an object.

In the case of the while in modifier position, it looks like it's
equivalent to this:

   while <some_test>
     ind += 1
   end

and it is. I prefer this to having it be equivalent to:

   loop do x += 1; break x if <some_test>; end


David

-- 
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE                   Ruby training with Black/Brown/McAnally
     COMPLEAT          Coming to Chicago area, June 18-19, 2010!
              RUBYIST  http://www.compleatrubyist.com