beefcake: A sane Google Protocol Buffers library for Ruby

Wynn Netherland Wynn Netherland

Needing an uber efficient means of transporting and storing data, Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

Not satisfied with the current support for Protocol Buffers in Ruby, Sinatra creator Blake Mizerany has created Beefcake. Built to feel like Ruby, Beefcake also supports non-blocking tools like EventMachine.

To create a protocol format, just create a Ruby class that inherits from Beefcake::Message:

require 'beefcake'

class Point < Beefcake::Message
  required :x, :int32, 1
  required :y, :int32, 2
  optional :tag, :string, 3

point = :x => 1, :y => 2
# or
point =
point.x = 1
point.y = 2

Any Ruby type that respones to << can accept encoding:

s = ""
p [:s, s]
# or (because encode returns the string/stream)
p [:s, point.encode("")]
# or
open("point.dat") do |f|

# decode
encoded = point.encode("")
decoded = Point.decode(encoded)
p [:point, decoded]

Blake is looking to add .proto file parsing, Enums, and wire types 1,3,5. Want to help out? Submit a patch.

[Source on GitHub]

0:00 / 0:00