I want to read data records from a non-seekable stream in Ruby. I want to leave it up to the user whether the current record should be skipped or read. Usually you would include Enumerable and provide the necessary methods for it to have an iterator in Ruby, but in my case data can be skipped and only one iteration is possible, so I’m not sure what I should do.
Which one would you prefer?
while record = foo.next
if bar
foo.read
else
foo.skip
end
end
or
foo.each do |record|
if bar
foo.read
else
foo.skip
end
end
Lazy evaluation?
Sounds like a good candidate for lazy evaluation.
How about yielding a record to the block that implements each accessor with lazy evaluation and reads the record if it gets used.
If there are a lot of accessors, you could just yield an attribute method that checks to see if the read processing has been done, does it if necessary, and then returns the record.
By the way, what’s the motivation for skip vs read? Execution speed? If so I might just try it the simple way and always read, just to see if it’s fast enough.