开发者

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

开发者 https://www.devze.com 2023-02-01 08:02 出处:网络
Very basic question on adding a number rank in front of results for a ru开发者_运维技巧by loop.

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

精彩评论

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