NOTE: This is alpha version now. Please don't use for much serious purpose.
What's this?
Protocol Buffers for Ruby.
Source
http://code.google.com/p/ruby-protobuf/source/browse/#svn/trunk/lib/protobuf
Install
sudo gem install ruby_protobuf
Quick Examples
Generate Ruby Code
A .proto file like this:
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}generates ruby code like this:
module Tutorial
class Person < ::Protobuf::Message
required :string, :name, 1
required :int32, :id, 2
optional :string, :email, 3
end
endby the command:
rprotoc PROTOFILE
Use Generated Ruby Code
Serialize a Message
person = Tutorial::Person.new person.id = 1234 person.name = 'John Doe' person.email = 'jdoe@example.com' phone = Tutorial::Person::PhoneNumber.new phone.number = '555-4321' phone.type = Tutorial::Person::PhoneType::HOME person.phone << phone serialized_string = person.serialize_to_string # person.serialize_to_file 'person.dat'
Parse a Serialized String/File
person = Tutorial::Person.new person.parse_from_string serialized_string #person.parse_from_file 'person.dat' assert_equal 1234, person.id assert_equal 'John Doe', person.name assert_equal 'jdoe@example.com', person.email assert_equal 1, person.phone.size assert_equal '555-4321', person.phone[0].number assert_equal Tutorial::Person::PhoneType::HOME, person.phone[0].type
For more examples, see the tutorial.