class ReverseCharIterator
def initialize(str)
@str = str
@idx = str.length - 1
end
def next
begin
@str[@idx]
ensure
@idx -= 1
end
end
def has_next?
@idx >= 0
end
end
class ReverseCharSet
def initialize(str)
@str = str
end
def iterator
ReverseCharIterator.new(@str)
end
end
#### main ####
char_set = ReverseCharSet.new('abcdefghijklmn')
iterator = char_set.iterator
while iterator.has_next?
puts iterator.next
end
Y2xhc3MgUmV2ZXJzZUNoYXJJdGVyYXRvcgogIGRlZiBpbml0aWFsaXplKHN0cikKICAgIEBzdHIgPSBzdHIKICAgIEBpZHggPSBzdHIubGVuZ3RoIC0gMQogIGVuZAoKICBkZWYgbmV4dAogICAgYmVnaW4KICAgICAgQHN0cltAaWR4XQogICAgZW5zdXJlCiAgICAgIEBpZHggLT0gMQogICAgZW5kCiAgZW5kCgogIGRlZiBoYXNfbmV4dD8KICAgIEBpZHggPj0gMAogIGVuZAplbmQKCmNsYXNzIFJldmVyc2VDaGFyU2V0CiAgZGVmIGluaXRpYWxpemUoc3RyKQogICAgQHN0ciA9IHN0cgogIGVuZAoKICBkZWYgaXRlcmF0b3IKICAgIFJldmVyc2VDaGFySXRlcmF0b3IubmV3KEBzdHIpCiAgZW5kCmVuZAoKIyMjIyBtYWluICMjIyMKIApjaGFyX3NldCA9IFJldmVyc2VDaGFyU2V0Lm5ldygnYWJjZGVmZ2hpamtsbW4nKQppdGVyYXRvciA9IGNoYXJfc2V0Lml0ZXJhdG9yCiAKd2hpbGUgaXRlcmF0b3IuaGFzX25leHQ/CiAgcHV0cyBpdGVyYXRvci5uZXh0CmVuZA==