Robert Dober
2/14/2009 7:33:00 PM
On Sat, Feb 14, 2009 at 7:16 AM, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> At Sat, 14 Feb 2009 15:03:03 +0900,
> Vetrivel Vetrivel wrote in [ruby-talk:328175]:
>> what is the Difference between loop and while in ruby ?
>>
>> loop do
>> end
>
> loop is a kernel method which takes a block. A block
> introduces new local variable scope.
>
> loop do
> a = 1
> break
> end
> p a #=> causes NameError
>
>> while 1
>> end
>
> while doesn't.
>
> while 1
> a = 1
> break
> end
> p a #=> 1
>
> --
> Nobu Nakada
>
furthermore loop do has an implicit rescue clause for a StopIteration exception
(I believe 1.8.7 and 1.9.1 only IIRC)
therefore
loop do
some_enumerator.next
end
becomes a convenient idiom.
HTH
Robert
--
It is change, continuing change, inevitable change, that is the
dominant factor in society today. No sensible decision can be made any
longer without taking into account not only the world as it is, but
the world as it will be ... ~ Isaac Asimov