diff --git a/lib/turing_tarpit.rb b/lib/turing_tarpit.rb index 0dc4962..cf86b22 100644 --- a/lib/turing_tarpit.rb +++ b/lib/turing_tarpit.rb @@ -40,7 +40,8 @@ def initialize(source_text) end def next(cell_value) - raise StopIteration if @chars.length == @index + validate_index + element = @chars[@index] case element @@ -51,10 +52,10 @@ def next(cell_value) element = @chars[@index] when "]" if cell_value.zero? - while @chars[@index] == "]" + while element == "]" consume element = @chars[@index] - raise StopIteration if @chars.length == @index + validate_index end else jump_back @@ -68,6 +69,10 @@ def next(cell_value) end private + + def validate_index + raise StopIteration if @chars.length == @index + end def consume @index += 1