From: dblack@... Date: 2003-08-13T01:27:32+09:00 Subject: Re: Elegant solution for a loop-break problem Hi -- On Wed, 13 Aug 2003, Austin Ziegler wrote: > 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) > > > for element in array > > if element==value > > puts "#{element} found in array!" > > break > > end > > else > > puts "#{element} not found in array" > > end > > This could be done with: > > if array.find { |e| e == element } > puts "#{element} found in array!" > else > puts "#{element} not found in array!" > end > > Alternatively: > > if array.include?(element) > puts "#{element} found in array!" > else > puts "#{element} not found in array!" > end Or even: puts "#{element}#{" not" unless array.include?(element)} found in array!" (sorry, couldn't resist :-) David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav