Name: ruby-protocol-buffers
Owner: nexiahome
Description: An implementation of Protocol Buffers for Ruby.
Created: 2013-11-30 05:21:04.0
Updated: 2016-11-10 20:33:36.0
Pushed: 2017-11-15 21:40:58.0
Homepage: http://code.mozy.com/
Size: 370
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.
This library has two components: a compiler to turn .proto
definitions
into Ruby modules (extension .pb.rb
), and a runtime to use protocol
buffers defined by these modules.
The compiler relies on Google's C++ based compiler (protoc
) for much
of the heavy lifting – this has huge advantages in ensuring
compatibility and correctness. If you don't need cross-language
interoperability you can create Message classes directly in ruby,
in which case protoc
is not needed. See “Writing Message Classes
Directly” below.
This library is heavily optimized for encoding and decoding speed.
Because this is a tool for generating code, the RDoc documentation is a bit unusual. See the text in the ProtocolBuffers::Message class for details on what code is generated.
$ gem install ruby-protocol-buffers
If you want to compile .proto files to ruby, you'll need protoc
version >= 2.2 (the Google Protocol Buffer compiler)
installed in the environment where you will be compiling them.
You do not need protoc
installed to use the generated .pb.rb
files.
For greater performance, consider installing the varint gem as well. This optional gem builds a small C extension to make parsing protocol buffers faster. If your application uses a Gemfile, add varint to the Gemfile alongside ruby-protocol-buffers.
Given the file test.proto:
age Test;
age MyMessage
tional string myField = 1;
Compile it to ruby using the command:
$ ruby-protoc test.proto
Then it can be used from ruby code:
ire 'test.pb'
= Test::MyMessage.new(:myField => 'zomgkittenz')
("test_msg", "wb") do |f|
g.serialize(f)
ded = msg.serialize_to_string # or msg.to_s
::MyMessage.parse(encoded) == msg # true
Protocol Buffer definitions are often shared between applications
written in different programming languages, and so are normally defined
in .proto files and translated to ruby using the ruby-protoc
binary.
However, it's quite simple to write ProtocolBuffers::Message classes directly when a .proto file isn't needed.
ire 'protocol_buffers'
s User < ProtocolBuffers::Message
quired :string, :name, 1
quired :string, :email, 2
tional :int32, :logins, 3
s Group < ProtocolBuffers::Message
peated User, :users, 1
peated Group, :subgroups, 2
dule GroupType
include ProtocolBuffers::Enum
Study = 1
Play = 2
d
tional GroupType, :group_type, 3
This code is essentially equivalent to the code ruby-protoc
will
generate if given this .proto file:
age User
quired string name = 1;
quired string email = 2;
tional int32 logins = 3;
age Group
peated User users = 1;
peated Group subgroups = 2;
um GroupType {
Study = 1;
Play = 2;
tional GroupType group_type = 3;
Using a hand-written Message subclass is the same as using a Message
class generated by ruby-protoc
.
p = Group.new(:group_type => Group::GroupType::Play)
p.users << User.new(:name => 'test user', :email => 'test@example.com')
("group1.test", "wb") do |f|
oup.serialize(f)
Brian Palmer (http://github.com/codekitchen)
http://github.com/mozy/ruby-protocol-buffers
See the LICENSE file included with the distribution for licensing and copyright details.