Reading/greping a file in ruby more than once

file, ruby

Solution

Use rewind

Positions ios to the beginning of input, resetting lineno to zero.

f = File.new("testfile") 
f.readline   #=> "This is line one\n"
f.rewind     #=> 0 
f.lineno     #=> 0 
f.readline   #=> "This is line one\n"

Problem

How can I reset the "pointer" of a file to its start without reopening it? (Something like `fseek` in C?) For example, I have a file that I want to grep for two patterns: ``` f=open('test') => #<File:test> f.grep(/llo/) => ["Hello world\n"] f.grep(/wo/) => [] ``` Is it possible to reset `f` without reopening the file? Note: I am not looking for workarounds; I can think some on my own ;).

Original source