From: Rob Biedenharn Date: 2008-07-17T10:56:57+09:00 Subject: Re: The next number that is not in an array On Jul 16, 2008, at 9:38 PM, Harry Kakueki wrote: > On Thu, Jul 17, 2008 at 6:01 AM, 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? >> Thanks in advance >> > # If your range is not too big, you could try this. > # It is not so efficient, but.... > > ngnums = [2,3,4,5,8,10,15,29] > mynum = 1 > > p (0..100_000).select {|x| x > mynum and ngnums.include?(x) == false} > [0] > > Harry > > -- > A Look into Japanese Ruby List in English > http://www.kakueki.com/ruby/list.html Change that to: p (0..100_000).detect {|x| x > mynum and ngnums.include?(x) == false} and it doesn't have to go all the way to the end of the range. You can use the alias find if that makes more sense than detect. p (0..100_000).find {|x| x > mynum and ngnums.include?(x) == false} -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com