fork download
  1. class Array
  2. def find_index_r(&block)
  3. Enumerator.new do |yielder|
  4. each_with_index do |item, idx|
  5. yielder.yield(idx) if block.call(item)
  6. end
  7. end
  8. end
  9. end
  10.  
  11. ary = [1,2,3,4,5,6]
  12.  
  13. e = ary.find_index_r{|r| r % 2 == 0}
  14. p e.to_a
  15. #=> [1, 3, 5]
  16.  
  17. # Modify the array
  18. ary << 8
  19. # We need to rewind as enumerator is not consistent with array
  20. e.rewind
  21.  
  22. p e.to_a
  23. #=>[1, 3, 5, 6]
Success #stdin #stdout 0.05s 9664KB
stdin
Standard input is empty
stdout
[1, 3, 5]
[1, 3, 5, 6]