Sha256: 31091df901fd579aa2bea2a1df7c055fc27ad8f91cbc8f8aa5e0d242187a29b2

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# What is BinData?

Do you ever find yourself writing code like this?

```ruby
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.

There is a better way. Here’s how you’d write the above using BinData.

```ruby
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}"
```

BinData provides a _declarative_ way to read and write structured binary data.

This means the programmer specifies *what* the format of the binary
data is, and BinData works out *how* to read and write data in this
format.  It is an easier (and more readable) alternative to
ruby's `#pack` and `#unpack` methods.

BinData makes it easy to create new data types. It supports all the common
primitive datatypes that are found in structured binary data formats. Support
for dependent and variable length fields is built in. 

# Installation

    $ gem install bindata

-or-

    $ sudo ruby setup.rb

# Documentation

[Read the wiki](http://github.com/dmendel/bindata/wiki).

-or-

    $ rake manual

# Contact

If you have any queries / bug reports / suggestions, please contact me
(Dion Mendel) via email at dion@lostrealm.com

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bindata-1.8.3 README.md
bindata-1.8.2 README.md
bindata-1.8.1 README.md
bindata-1.8.0 README.md