class CharEnumerator
include Enumerable
def initialize( str)
@str = str
@idx = 0
end
def each( & block)
while @idx < @str .length
yield @str [ @idx]
@idx + = 1
end
end
end
#### main ####
puts '***************'
CharEnumerator.new ( 'abcdefghijklmn' ) .each do | ch|
puts ch
end
puts '***************'
### Enumerableをincludeすると、each以外にも便利なもAPIが使える
CharEnumerator.new ( 'abcdefghijklmn' ) .each_with_index do | ch, idx|
puts "#{idx+1}文字目は#{ch}"
end
Y2xhc3MgQ2hhckVudW1lcmF0b3IKICBpbmNsdWRlIEVudW1lcmFibGUKICAKICBkZWYgaW5pdGlhbGl6ZShzdHIpCiAgICBAc3RyID0gc3RyCiAgICBAaWR4ID0gMAogIGVuZAoKICBkZWYgZWFjaCgmYmxvY2spCiAgICB3aGlsZSBAaWR4IDwgQHN0ci5sZW5ndGgKICAgICAgeWllbGQgQHN0cltAaWR4XQogICAgICBAaWR4ICs9IDEKICAgIGVuZAogIGVuZAplbmQKCiMjIyMgbWFpbiAjIyMjCgpwdXRzICcqKioqKioqKioqKioqKionCkNoYXJFbnVtZXJhdG9yLm5ldygnYWJjZGVmZ2hpamtsbW4nKS5lYWNoIGRvIHxjaHwKICBwdXRzIGNoCmVuZAoKcHV0cyAnKioqKioqKioqKioqKioqJwojIyMgRW51bWVyYWJsZeOCkmluY2x1ZGXjgZnjgovjgajjgIFlYWNo5Lul5aSW44Gr44KC5L6/5Yip44Gq44KCQVBJ44GM5L2/44GI44KLCkNoYXJFbnVtZXJhdG9yLm5ldygnYWJjZGVmZ2hpamtsbW4nKS5lYWNoX3dpdGhfaW5kZXggZG8gfGNoLCBpZHh8CiAgcHV0cyAiI3tpZHgrMX3mloflrZfnm67jga8je2NofSIKZW5kCgo=