[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

while vs loop

Vetrivel Vetrivel

2/14/2009 6:03:00 AM

what is the Difference between loop and while in ruby ?

loop do
end

while 1
end
--
Posted via http://www.ruby-....

4 Answers

Chris Kottom

2/14/2009 6:08:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Loop will continue indefinitely until it meets with some break statement
somewhere in its loop body.

While can also be terminated with a break statement, but more traditionally,
it ends when the condition specified in the while statement is satisfied.
If you have "while 1" as you've written here though, the two should be
functionally equivalent.


On Sat, Feb 14, 2009 at 7:03 AM, Vetrivel Vetrivel <vetrivel.bksys@gmail.com
> wrote:

> what is the Difference between loop and while in ruby ?
>
> loop do
> end
>
> while 1
> end
> --
> Posted via http://www.ruby-....
>
>

Nobuyoshi Nakada

2/14/2009 6:16:00 AM

0

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

Robert Dober

2/14/2009 7:33:00 PM

0

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

Bertram Scharpf

2/20/2009 10:18:00 PM

0

Hi,

Am Samstag, 14. Feb 2009, 15:16:16 +0900 schrieb Nobuyoshi Nakada:
> 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 is a kernel method which takes a block. A block
> introduces new local variable scope.
>
> while doesn't.
>

In other words:

$ irb
irb(main):001:0> Kernel.private_instance_methods.grep /loop/
=> ["loop"]
irb(main):002:0> Kernel.private_instance_methods.grep /while/
=> []

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...