1 This directory contains the Ruby extension that implements Protocol Buffers
4 The Ruby extension makes use of generated Ruby code that defines message and
5 enum types in a Ruby DSL. You may write definitions in this DSL directly, but
6 we recommend using protoc's Ruby generation support with .proto files. The
7 build process in this directory only installs the extension; you need to
8 install protoc as well to have Ruby code generation functionality.
13 When we release a version of Protocol Buffers, we will upload a Gem to
14 [RubyGems](https://www.rubygems.org/). To use this pre-packaged gem, simply
15 install it as you would any other gem:
17 $ gem install [--prerelease] google-protobuf
19 The `--pre` flag is necessary if we have not yet made a non-alpha/beta release
20 of the Ruby extension; it allows `gem` to consider these "pre-release"
23 Once the gem is installed, you may or may not need `protoc`. If you write your
24 message type descriptions directly in the Ruby DSL, you do not need it.
25 However, if you wish to generate the Ruby DSL from a `.proto` file, you will
26 also want to install Protocol Buffers itself, as described in this repository's
27 main `README` file. The version of `protoc` included in the latest release
28 supports the `--ruby_out` option to generate Ruby code.
30 A simple example of using the Ruby extension follows. More extensive
31 documentation may be found in the RubyDoc comments (`call-seq` tags) in the
32 source, and we plan to release separate, more detailed, documentation at a
35 require 'google/protobuf'
37 # generated from my_proto_types.proto with protoc:
38 # $ protoc --ruby_out=. my_proto_types.proto
39 require 'my_proto_types'
41 mymessage = MyTestMessage.new(:field1 => 42, :field2 => ["a", "b", "c"])
43 mymessage.field2.push("d")
44 mymessage.field3 = SubMessage.new(:foo => 100)
46 encoded_data = MyTestMessage.encode(mymessage)
47 decoded = MyTestMessage.decode(encoded_data)
48 assert decoded == mymessage
51 puts MyTestMessage.encode_json(mymessage)
53 Installation from Source (Building Gem)
54 ---------------------------------------
56 To build this Ruby extension, you will need:
60 * Ruby development headers
63 To Build the JRuby extension, you will need:
66 * The latest version of the protobuf java library (see ../java/README.md)
67 * Install JRuby via rbenv or RVM
69 First switch to the desired platform with rbenv or RVM.
71 Then install the required Ruby gems:
79 $ rake clobber_package gem
80 $ gem install `ls pkg/google-protobuf-*.gem`
86 This gem includes the upb parsing and serialization library as a single-file
87 amalgamation. It is up-to-date with upb git commit
88 `535bc2fe2f2b467f59347ffc9449e11e47791257`.
93 We are using a version number scheme that is a hybrid of Protocol Buffers'
94 overall version number and some Ruby-specific rules. Gem does not allow
95 re-uploads of a gem with the same version number, so we add a sequence number
96 ("upload version") to the version. We also format alphabetical tags (alpha,
97 pre, ...) slightly differently, and we avoid hyphens. In more detail:
99 * First, we determine the prefix: a Protocol Buffers version "3.0.0-alpha-2"
100 becomes "3.0.0.alpha.2". When we release 3.0.0, this prefix will be simply
102 * We then append the upload version: "3.0.0.alpha.2.0" or "3.0.0.0". If we need
103 to upload a new version of the gem to fix an issue, the version becomes
104 "3.0.0.alpha.2.1" or "3.0.0.1".
105 * If we are working on a prerelease version, we append a prerelease tag:
106 "3.0.0.alpha.3.0.pre". The prerelease tag comes at the end so that when
107 version numbers are sorted, any prerelease builds are ordered between the
108 prior version and current version.
110 These rules are designed to work with the sorting rules for
111 [Gem::Version](http://ruby-doc.org/stdlib-2.0/libdoc/rubygems/rdoc/Gem/Version.html):
112 release numbers should sort in actual release order.