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/.