From: Julian Leviston Date: 2009-02-10T21:29:43+09:00 Subject: Re: a better way to do this job? Slices and 'sub-arrays' aren't copies of the objects, they simply refer to the identical objects. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 10/02/2009, at 10:37 PM, 7stud -- wrote: > Zhenning Guan wrote: >> topics.each do |f| >> times +=1 >> puts f.title >> if times > 4 >> break >> > >>> Another option, which can be used for any Enumerable object is: >>> >>> topics.each_with_index do |f, i| >>> puts f.title >>> break if i > 4 >>> end >>> >>> While not as nice as the first version, it's a bit clearer than your >>> code. >>> > > In my opinion, it's nicer than the first version because it doesn't > have > to create a sub array in memory, like all the solutions posted so far > do, which could be a problem with large arrays and large slices. > > 4.times do |i| > puts topics[i].title > end > > -- > Posted via http://www.ruby-forum.com/. >