fork download
  1. class ReverseCharIterator
  2. def initialize(str)
  3. @str = str
  4. @idx = str.length - 1
  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 >= 0
  17. end
  18. end
  19.  
  20. class ReverseCharSet
  21. def initialize(str)
  22. @str = str
  23. end
  24.  
  25. def iterator
  26. ReverseCharIterator.new(@str)
  27. end
  28. end
  29.  
  30. #### main ####
  31.  
  32. char_set = ReverseCharSet.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
n
m
l
k
j
i
h
g
f
e
d
c
b
a