I am trying to do file.each_char right after a file.each_line, but it never gets called when it is like this. If I get rid of the file.each_line, the file.each_char call works perfectly.
Here’s my code for reference:
file.each_line do |line|
if line =~ /^\s*$/
next
end
lines += 1
end
file.each_char do |char|
if char =~ /\s/
next
end
chars += 1
end
How can I manage a file.each_char call right after file.each_line?
When you run
each_line, it leaves it pointing at the end of your IO stream (in this case a file). To iterate over the whole file a second time, you’ll need to reset it to point at the beginning of the stream.IO#rewindwill do this for you: