From: Tanaka Akira Date: 2009-02-04T13:47:52+09:00 Subject: [ruby-core:21824] Re: [Feature #908] Should be an easy way of reading N characters from am I/O stream In article <49884f719007d_84959c52d4271d@redmine.ruby-lang.org>, James Gray writes: > 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. Ruby has similar situation. Ruby peeks magic comments of a script. It reads some bytes and put back it to IO buffer. It is done on byte layer. IO#read(n) and IO#ungetbyte is usable. For line terminator detection, we need encoding conversion because line terminator is defined in character layer, not byte layer. Since the data may be incomplete, String#encode may cause an error. Encoding::Converter can be used instead. -- Tanaka Akira