From: David Holroyd Date: 2006-02-22T22:42:52+09:00 Subject: Re: Question about String.unpack On Wed, Feb 22, 2006 at 04:19:07AM +0900, Jacob Repp wrote: > I'm curious about binary protocol parsing in ruby and have scratched > around for some kind of BinaryReader-ish interface for strings. Seems > like you could solve this if you know that your struct is in a string > you could say: > > str = fd.sysread(n) > name = str.read_str(30) > size = str.read_double > age = str.read_int > stuff = str.read_c > > I was going to write a native extension to do this after I finish my > current Ruby/AIO extension project. Unless there is something that > does this already (maybe in a cleaner/more ruby-ish way?) Yes, I've needed one of these too (read + write). My application was handling a binary UDP protocol. String#unpack doesn't support some of the bizarro data formating that this protocol seems to have, so I cooked up ReadBuffer/WriteBuffer[1] classes with a few methods a bit like those above. ...just my 2-pence-use-case. dave [1] http://svn.badgers-in-foil.co.uk/alchemic/trunk/na_protocol.rb -- http://david.holroyd.me.uk/