From: "David A. Black" Date: 2009-02-11T22:00:36+09:00 Subject: Re: a better way to do this job? Hi -- On Wed, 11 Feb 2009, Sebastian Hungerecker wrote: > David A. Black wrote: >> Hi -- >> >> On Tue, 10 Feb 2009, Julian Leviston wrote: >>> Slices and 'sub-arrays' aren't copies of the objects, they simply refer >>> to the identical objects. >> >> The same can be said of any array; the objects inside it exist (in >> many cases, at least) already. Still, container objects do take up >> memory. > > Ruby arrays are copy-on-write though. So subarray = my_array[100...1000100] > does *not* take up any memory except for the additional array object (which > will contain a pointer to the 100th item of my_array and the length 1000000). My memory-eating example was probably unrealistic. Maybe I'm too steeped in the idea that enumerators are good because they don't create intermediate arrays.... (I have some real issues with some of what they do, so I tend to cling to that one.) Ryan's point (it's not a problem until it's a problem) is a key one, in any case. I definitely would not discourage creation of intermediate objects unless there's a real reason to avoid them. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!