From: Jason Creighton Date: 2003-08-08T01:46:22+09:00 Subject: Re: Elegant solution for a loop-break problem On Fri, 8 Aug 2003 00:00:15 +0900 KONTRA Gergely wrote: > > > Is there something similar to python's for .. else ... structure? (Do > > > something, ONLY when the loop is terminated normally (without break) > > Eg. suppose I want to search for value in array > If it would be a for .. else .. end construction in ruby, I could write: > > for element in array > if element==value > puts "#{element} found in array!" > break > end > else > puts "#{element} not found in array" > end If I were you, I'd just use the #include? method to see if an array contains an element. Or, I might search for an element in the array like so: > >> array = [1,2,4,9] => [1, 2, 4, 9] >> array.detect { |e| e % 2 == 0 } => 2 >> array.detect { |e| e == 10 } => nil >> array.detect { |e| e/2 > 2 } => 9 ....and so on. Except you're talking about break, so this is all beside the point. Jason Creighton