From: Stephen Schor Date: 2008-04-15T22:41:15+09:00 Subject: Re: noob 'why doesn't this work' question ------=_Part_901_2758719.1208266873520 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline sizeList = [0,1,2,3,4,5,6] countarray = [3,4,6] countarray.each {|x| sizeList.delete_at(x)} Let's look at what's going on inside your .each block. The first iteration it will delete the sizeList[3]. ===> [0,1,2,4,5,6] The second iteration it will delete sizeList[4] ===> [0,1,2,4,6] The final iteration it will delete sizeList[6] ==> [0,1,2,4,6] #there is no sizeList[6] - deleteAt returns nil. Your indexs are changing each time you call delete_at. -Stephen On Tue, Apr 15, 2008 at 9:15 AM, Robin Pedersen wrote: > On Apr 14, 5:47 pm, Vincent Angeloni wrote: > > Hi and greetings to all group members! > > > > I'm a new Ruby user with a background in Applescript, Hypertalk > > (Supercard), and a little Unix. I was intrigued by Matt Neuburg's > > article about Applescript and Ruby, and that's what got me reading some > > intro books on Ruby. I just bought Textmate and I am lovin' it! > > > > Anyway, I am wondering why this seemingly simple script fails: > > > > sizeList = [0,1,2,3,4,5,6] > > countarray = [3,4,6] > > countarray.each {|x| sizeList.delete_at(x)} > > > > what I want to happen is that sizelist gets deleted at the positions > > specified in countArray, but the result I am getting is: > > > > 0 > > 1 > > 2 > > 4 > > 6 > > > > and the expected result should be 0,1,2,5, right? > > What gives? > > > > TIA, > > vince > > > > Mac OS X 10.5.2, G5 Quad, Textmate editor, ruby 1.86 > > What about this: > > sizeList = [0,1,2,3,4,5,6] > countarray = [3,4,6] > countarray.sort.reverse.each {|x| sizeList.delete_at(x)} > > Result: 0,1,2,5 > > ------=_Part_901_2758719.1208266873520--