A declarative way to read and write structured binary data in Ruby.
BinData is a Ruby gem described in its documentation as:
A declarative way to read and write structured binary data. ... It supports all the common datatypes that are found in structured binary data. Support for dependent and variable length fields is built in.
Again, from the documentation, instead of writing code looking like:
io = File.open(...)
len = io.read(2).unpack("v")[0]
name = io.read(len)
width, height = io.read(8).unpack("VV")
puts "Rectangle #{name} is #{width} x #{height}"
BinData lets you rewrite it in a more idiomatic and readable way:
class Rectangle < BinData::Record
endian :little
uint16 :len
string :name, :read_length => :len
uint32 :width
uint32 :height
end
io = File.open(...)
r = Rectangle.read(io)
puts "Rectangle #{r.name} is #{r.width} x #{r.height}"