没有任何数据可供显示
开源项目社区 | 当前位置 : |
|
www.trustie.net/open_source_projects | 主页 > 开源项目社区 > ruby-protobuf |
ruby-protobuf
|
0 | 0 | 215 |
贡献者 | 讨论 | 代码提交 |
What's this?Protocol Buffers for Ruby.
Sourcehttp://code.google.com/p/ruby-protobuf/source/browse/#svn/trunk/lib/protobuf http://github.com/macks/ruby-protobuf (experimental) Installsudo gem install ruby_protobufQuick ExamplesGenerate Ruby CodeA .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:
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/Fileperson = 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.phone0.number)
assert_equal(Tutorial::Person::PhoneType::HOME, person.phone0.type)For more examples, see the tutorial.