Posted By: Dion Mendel
Date: 2007-03-17 23:07
Summary: BinData 0.5.0
Project: BinData

Do you ever find yourself writing code like this?

io = File.open(...)
len = io.read(2).unpack("v")
name = io.read(len)
width, height = io.read(8).unpack("VV")
puts "Rectangle #{name} is #{width} x #{height}"

It's ugly, violates DRY and feels like you're writing Perl, not Ruby.

BinData is a declarative way of reading and writing structured binary data. Here's how you'd write the above using BinData.

class Rectangle < BinData::Struct
. uint16le :len
. string :name, :initial_length => :len
. uint32le :width
. uint32le :height
end

io = File.open(...)
r = Rectangle.read(io)
puts "Rectangle #{r.name} is #{r.width} x #{r.height}"

BinData supports signed/unsigned integers, strings, null terminated strings, arrays, choices and user defined structures. This initial release has been heavily tested, and is being actively developed.

Latest News
TZInfo::Data v1.2014.2 Released
    Philip Ross - 2014-03-25 22:32
TZInfo v0.3.39 Released
    Philip Ross - 2014-03-09 20:23
TZInfo::Data v1.2014.1 Released
    Philip Ross - 2014-03-09 20:00
Automatic Ruby 14.2.0 has been released!
    id 774 - 2014-02-26 06:23
kramdown 1.3.2 released
    Thomas Leitner - 2014-02-16 08:35

 

Forums | Admin

Discussion Forums: bindata-0.5.0

Start New Thread Start New Thread

 

Topic Topic Starter Replies Last Post