From: "David A. Black" Date: 2008-07-17T08:51:26+09:00 Subject: Re: The next number that is not in an array Hi -- On Thu, 17 Jul 2008, Tim Conner wrote: > I want to increment the current value of a variable to the next number > that does not appear in an array of restricted numbers. > e.g. > restricted_numbers = [2,3,4,5,8,10,15,29] > > if the variable is currently storing 11 then I want to increment it to > 12, > however if the variable is currently storing 1 then I want to increment > it to 6. > > How is this done in ruby? Here's a pretty terse way -- maybe too terse, and rather side-effect-ish, but kind of interesting: true while restricted_numbers.include?(n+=1) This (and I think the other versions) keeps incrementing off the end of the array, so you'd have to add something to make that not happen. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates!