views:

24

answers:

1

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
+2  A: 

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.

foo.each do |record_manager|
  if bar
    puts record_manager.get.field1 # get does foo.read, whatever that does
    puts record_manager.get.field2 # just returns the processed object this time
  else
    # each() will do .skip() if no get() was called
  end
end

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.

DigitalRoss
The motivation is that the data is fairly large.
ott