开发者

Add number rank in front of basic ruby loop so that each result has a number in front of it

Very basic question on adding a number rank in front of results for a ru开发者_运维技巧by loop.

@model.each do |foo|
#code here to put the number of the element in the loop starting at 1 and going up.  
puts foo.title
  end

Which would ideally print the following results.

1 titlea
2 titleb
3 titlec

haven't been able to find this anywhere - any help is appreciated.

Thanks


Depends on what @model is. If it's an Enumerable, you could do:

@model.each_with_index do |foo, i|
  puts "#{i} #{foo.title}"
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜