From: James Gray Date: 2009-02-03T23:06:18+09:00 Subject: [ruby-core:21803] [Feature #908] Should be an easy way of reading N characters from am I/O stream Issue #908 has been updated by James Gray. I needed this in the standard CSV library. My use case was that I peek ahead in the stream to determine what kind of line endings it has. I just grab a block of characters and see if I find any standard line endings in there. This was pretty challenging in Ruby 1.9, because just reading some bytes meant I had great chances to have picked up invalid data. Then, when I hit it with a Regexp to find the line endings, an Exception is raised. I'm using this code to get around that problem in CSV: # # Reads at least +bytes+ from @io, but will read up 10 bytes ahead if # needed to ensure the data read is valid in the ecoding of that data. This # should ensure that it is safe to use regular expressions on the read data, # unless it is actually a broken encoding. The read data will be returned in # @encoding. # def read_to_char(bytes) return "" if @io.eof? data = @io.read(bytes) begin encoded = encode_str(data) raise unless encoded.valid_encoding? return encoded rescue # encoding error or my invalid data raise if @io.eof? or data.size >= bytes + 10 return data else data += @io.read(1) until data.valid_encoding? or @io.eof? or data.size >= bytes + 10 retry end end end That worked for CSV, where I just need some characters and don't have to have an exact count. If you do need an exact count though, the code gets more complicated. I agree that this is something Ruby should do for us. ---------------------------------------- http://redmine.ruby-lang.org/issues/show/908 ---------------------------------------- http://redmine.ruby-lang.org