From: "framefritti@..." Date: 2008-03-04T00:04:56+09:00 Subject: Re: Normal For Loop Cory Cory ha scritto: > I understand that all of these for-each style loops are available, but I > don't want to always go through the entire loop, sometimes I want to > stop at some earlier condition. > > I tried to do this the other night, but right now I can't find a good > example so here it goes: > > a = some_array > minValue = 999999 > for(i=0; i minValue = (5000 - a[i]).abs > } > > > This isn't the best example. However, there are many times like the > loop above where I want to go through the whole thing, but if I find > exactly what I am looking for, I want to bail out early instead of > wasting that processing time. In this case you can use "break" construct (which I, personally, love). For example, minValue = 999999 a.each do |x| minValue = (5000-x).abs break if minValue==0 end (I did not actually try it, but it should work) > > Also, I sometimes may want to not actually iterate straight through, but > browse through the array in some more complex order. In this case I'm afraid that you must resort to a while... (BTW, I always considered C-style "for" as a "while" in disguise... ;-) > -- > Posted via http://www.ruby-forum.com/.