From: "David A. Black" Date: 2008-07-20T03:36:39+09:00 Subject: Re: The next number that is not in an array Hi -- On Sun, 20 Jul 2008, Bill Walton wrote: > 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. So like this? irb(main):020:0> a = [2,3,4,8,12,13,15] => [2, 3, 4, 8, 12, 13, 15] irb(main):021:0> ([*a[0]..20] - a).shift => 5 It's a cool idea, though I would find it somewhat hard to see what's going on at a glance than some of the others. > 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? Have you used the Benchmark module? It's the usual tool for getting comparisons of different snippets that do the same thing as each other. 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!