有時,我想使用Ruby的Enumerable#each_with_index
而不是Array#each
當我想跟蹤索引。有沒有像我可以使用Kernel#loop_with_index
而不是Kernel#loop
的方法?Ruby:循環與索引?
4
A
回答
3
您可以使用Fixnum#upto和Float::INFINITY。
0.upto(Float::INFINITY) do |i|
puts "index: #{i}"
end
不過,我可能只是使用Kernel#loop
和跟蹤指數的自己,因爲這似乎更簡單。
i = 0
loop do
puts "index: #{i}"
i += 1
end
所以,是的,我不認爲有什麼像Kernel#loop_with_index
。
11
循環沒有一個枚舉,其中有一個with_index
方法塊的結果(如果你喜歡的和each_with_index
。)
loop.with_index{|_, i| puts i; break if i>100}
0
在最近的Ruby版本,Numeric#step具有無限和步驟的缺省限制大小爲1.
0.step{|i| puts i ; break if i>100 }
相關問題
- 1. Hpricot與索引循環?
- 2. Ruby中的兩個索引for循環
- 3. Ruby - 帶索引和元素的循環
- 4. 循環索引
- 5. 索引循環XSL
- 6. @index循環索引
- 7. 薩斯與索引每個循環
- 8. 更改循環內的循環索引
- 9. For循環索引問題
- 10. 循環索引在迭代
- 11. 索引for循環python
- 12. knockout.js嵌套循環索引
- 13. 蟒蛇循環索引
- 14. PDO綁定循環索引
- 15. for-each循環索引
- 16. 替代循環和索引?
- 17. SCILAB循環無效索引
- 18. 無循環索引neigboring值
- 19. 循環索引溢出
- 20. numpy:ravel_multi_index遞增循環索引循環不同的結果循環
- 21. Ruby on Rails:對象數組括號中的循環和索引
- 22. python 2.7循環索引引號
- 23. Python:循環遍歷一個列表使用所有索引與每個索引
- 24. Ruby:循環與救援的區別
- 25. Ruby on Rails循環內的循環
- 26. 爲什麼工廠循環打印超出循環索引?
- 27. 多層循環中循環索引的Fortran邏輯語句
- 28. 從NSManagedObject循環內循環獲取索引
- 29. 更改循環內部的循環索引變量
- 30. 索引混淆:嵌套JavaScript循環forfor for循環