From: Michael Selig Date: 2009-02-24T13:51:23+09:00 Subject: [ruby-core:22395] Re: [Feature #908] Should be an easy way of reading N characters from am I/O stream On Tue, 24 Feb 2009 14:31:39 +1100, daz wrote: > In WallyCode - don't Google it - character codes are assigned > to common words, such as ... > > 0x112233 = "Hello" > 0x445566 = "World" I don't think that Ruby's multibyte encoding ever intended to support conversion of a single character to multiple characters. The conversion of the file's external encoding to internal is a one-to-one character mapping. In other words reading the character "Hello" as you describe would never involve the translation to the 5 separate characters "Hello", unless you wrote something to do the translation. > ... where a complete character code is 3-bytes long. If you provide > a buffer that's at least 6-bytes long, IO can return two valid codes > (the things you refer to as "characters"). IO#gets does NOT require a fixed length buffer to read into. It returns a string, which can be an arbitrary length. > Of course, the WallyString class will truncate the result to "HelloW" > but the IO pointer is one character code (3-bytes) too far advanced. Maybe your WallyString class will do that, but not IO#gets, which is what I am talking about. If you ask for 2 characters, you should get a 2 character string (unless you hit EOF). > Implementing "Read(N)" this at the wrong level could result in > compromising the extensibilty of the encoding system and create > problems I was NOT suggesting changing the current implmentation of IO#read to mean characters. It is just IO#gets. > but ... What is a "character"? I was using Ruby 1.9's definition. Of course you are free to write methods that make it mean anything you want, but that is not to say that these should be in the core libraries. Mike.