Stefano Crocco
2/10/2009 9:16:00 AM
Alle Tuesday 10 February 2009, Zhenning Guan ha scritto:
> topics.each do |f|
> times +=1
> puts f.title
> if times > 4
> break
>
>
> a little ugly, doesn't it?
If topics is an array, you can use:
topics[0..4].each{|f| puts f.title}
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.
I hope this helps
Stefano