fork download
  1. class CharIterator
  2. def initialize(str)
  3. @str = str
  4. @idx = 0
  5. end
  6.  
  7. def next
  8. begin
  9. @str[@idx]
  10. ensure
  11. @idx += 1
  12. end
  13. end
  14.  
  15. def has_next?
  16. @idx < @str.length
  17. end
  18. end
  19.  
  20. class CharSet
  21. def initialize(str)
  22. @str = str
  23. end
  24.  
  25. def iterator
  26. CharIterator.new(@str)
  27. end
  28. end
  29.  
  30. #### main ####
  31.  
  32. char_set = CharSet.new('abcdefghijklmn')
  33. iterator = char_set.iterator
  34.  
  35. while iterator.has_next?
  36. puts iterator.next
  37. end
Success #stdin #stdout 0.01s 8200KB
stdin
Standard input is empty
stdout
a
b
c
d
e
f
g
h
i
j
k
l
m
n