From: Todd Benson Date: 2009-04-27T14:05:13+09:00 Subject: Re: How to parse a "line"? On Sun, Apr 26, 2009 at 11:45 PM, 7stud -- wrote: > Todd Benson wrote: >> On Sun, Apr 26, 2009 at 11:03 PM, Todd Benson >> wrote: >>> to me if it did. >> Let me rephrase. What is different on the underlying structure with >> [1..-1] and #shift? I'm not sure if shift really moves everything, >> but if it does, I wouldn't mind knowing. >> >> Todd > > shift changes the array in place: I looked at an old thread where Bob Hutchinson piped in about potential GC problems with this, but that was back in '07 > > 1) > arr = [10, 20, 30] > > y = arr.shift > puts y > p arr > > --output:-- > 10 > [20, 30] > > > subscripts create a new array: > > 2) > arr = [10, 20, 30] > p arr > print "id: ", arr.object_id > puts > > x = arr[1..-1] new object is created by x = > p x > print "id: ", x.object_id > puts > p arr > > --output:-- > [10, 20, 30] > id: 75950 > [20, 30] > id: 75860 > [10, 20, 30] > >> Is that really what happens? I thought it just reassigned a starting >> point, and indexed from there. At least, that would make more sense >> to me if it did. > > Would it? What if you had an array that took up 3GB of memory and you > shifted off every element but the last one. Would you expect your array > to still occupy 3GB of memory? I would hope that the GC could keep up, and I'd plan accordingly if not. But if your 3GB array was duped/cloned, hmm, is that better? Please tell me we are talking about two different arguments. Todd