From: "Ara.T.Howard" Date: 2004-06-20T05:43:19+09:00 Subject: Re: [RCR] IO#readn On Sun, 20 Jun 2004, Michael Neumann wrote: > 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 but another read may give one more byte - then you'd have your 4? even if one implemented IO#read(n) i'd think you'd want: class IO def readn n buf = '' buf << read(n - buf.size) until buf.size == n buf end end which give you what you want (n bytes), or throws EOFError. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; and a weed grows, even though we do | not love it. --Dogen ===============================================================================