From: Bill Walton Date: 2008-07-20T02:57:23+09:00 Subject: Re: The next number that is not in an array Hi All, David A. Black wrote: > 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) I've been working in Rails for about 3 years now, but have only recently begun to really dig in to Ruby. I say that in hopes you'll answer a couple of questions to help me 'dig' in more productive locations :-) My first take on this 'quiz' was to initialize a new array with members between the current value and the largest restricted number (including the largest), subtract the restricted_number array from the new one, and return the first member of the result. None of the solutions proposed involved anything like this, so I'm hoping one or more of you will say a little bit about why. Second question has to do with comments Robert Dober made like '10 times slower' and '50 times faster'. I'm assuming there's some benchmarking capability I need to learn more about. Could somebody point me? Also, is this capability available in IRB? TIA, Bill