From: Michael Neumann Date: 2004-06-20T02:01:14+09:00 Subject: Re: [RCR] IO#readn On Sun, Jun 20, 2004 at 01:33:18AM +0900, Ara.T.Howard wrote: > On Sun, 20 Jun 2004, Michael Neumann wrote: > > >Hi, > > > >I suggest to add a method readn to the IO class (and to StringIO, > >Socket...), which behaves similar to read(n), but raises an exception if > >the returned string is not of size n. > > > > class IO > > def readn(n) > > str = read(n) > > raise if str.nil? or str.size != n > > str > > end > > end > > > >It's annoying to test for it in the application. > > > >Regards, > > > > Michael > > don't you think the code will be just as long/annoying with a begin/rescue > block? No, as I wouldn't use a rescue clause in this case. It's better to crash early than proceed with a wrong value. I never liked it that read(4) might return only 3 characters. Regards, Michael