This here is a Ruby wrapper for the official C++ implementation of Cap'n Proto.
First install libcapnp, then install the gem:
gem install capn_proto --preThe native extension for this gem requires a C++ compiler with C++11 features, so use the same C++ compiler and flags that you used to compile libcapnp (e.g. CXX and CXXFLAGS). As an OSX user, having followed the instructions for installing libcapnp on OSX, the correct incantation is as follows:
CXX=$HOME/clang-3.2/bin/clang++ gem install capn_proto --prerequire'capn_proto'moduleAddressBookextendCapnProto::SchemaLoaderload_schema("addressbook.capnp")enddefwrite_address_book(file)addresses=AddressBook::AddressBook.new_messagepeople=addresses.initPeople(2)alice=people[0]alice.id=123alice.name='Alice'alice.email='alice@example.com'alice_phones=alice.initPhones(1)alice_phones[0].number="555-1212"alice_phones[0].type='mobile'alice.employment.school="MIT"bob=people[1]bob.id=456bob.name='Bob'bob.email='bob@example.com'bob_phones=bob.initPhones(2)bob_phones[0].number="555-4567"bob_phones[0].type='home'bob_phones[1].number="555-7654"bob_phones[1].type='work'bob.employment.unemployed=niladdresses.write(file)enddefprint_address_book(file)addresses=AddressBook::AddressBook.read_from(file)addresses.people.eachdo |person|
puts"#{person.name} : #{person.email}"person.phones.eachdo |phone|
puts"#{phone.type} : #{phone.number}"endifperson.employment.unemployed?puts"unemployed"ifperson.employment.employer?puts"employer: #{person.employment.employer}"ifperson.employment.school?puts"student at: #{person.employment.school}"ifperson.employment.selfEmployed?puts"self employed"endendendif__FILE__ == $0
file=File.open("addressbook.bin","wb")write_address_book(file)file=File.open("addressbook.bin","rb")print_address_book(file)endWhat's implemented:
- Schema parsing/loading
- Message reading
- From byte string
- From file descriptor
- Message writing
- To byte string
- To file descriptor
What's to come:
- More reading/writing mechanisms:
- Packing/unpacking
- Extensive test coverage
- Proper support for JRuby
- Support for RPC


