I have an Enumerator in the middle of some sequence:
enum = (1..9).each
first = enum.next
second = enum.next
Now I want to cycle through the rest of the sequence (3..9 numbers). But seeming obvious solution (which works in python, e.g.), restart from the beginning of the sequence instead of the third item:
for item in enum
puts item
end
# prints 1..9 instead of 3..9
Working solution which I found seems ugly:
begin
while item=enum.next
puts item
end
rescue StopIteration
end
So the question: is there a nicer rubyish solution to do this thing? And why does for
loop in Ruby act this way?
rescue
is redundant, thanks, never knew that (which in turn means the whole construct is as unrubyish that I failed to try it in 5+ years experience :) – Delorenzo