Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 1 | This directory contains the Ruby extension that implements Protocol Buffers |
| 2 | functionality in Ruby. |
| 3 | |
| 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. |
| 9 | |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 10 | Installation from Gem |
| 11 | --------------------- |
| 12 | |
| 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: |
| 16 | |
| 17 | $ gem install [--prerelease] google-protobuf |
| 18 | |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 19 | Once the gem is installed, you may or may not need `protoc`. If you write your |
| 20 | message type descriptions directly in the Ruby DSL, you do not need it. |
| 21 | However, if you wish to generate the Ruby DSL from a `.proto` file, you will |
| 22 | also want to install Protocol Buffers itself, as described in this repository's |
| 23 | main `README` file. The version of `protoc` included in the latest release |
| 24 | supports the `--ruby_out` option to generate Ruby code. |
| 25 | |
| 26 | A simple example of using the Ruby extension follows. More extensive |
| 27 | documentation may be found in the RubyDoc comments (`call-seq` tags) in the |
| 28 | source, and we plan to release separate, more detailed, documentation at a |
| 29 | later date. |
| 30 | |
Josh Haberman | 90c7f6e | 2016-04-14 12:48:41 -0700 | [diff] [blame] | 31 | ```ruby |
| 32 | require 'google/protobuf' |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 33 | |
Josh Haberman | 90c7f6e | 2016-04-14 12:48:41 -0700 | [diff] [blame] | 34 | # generated from my_proto_types.proto with protoc: |
| 35 | # $ protoc --ruby_out=. my_proto_types.proto |
| 36 | require 'my_proto_types' |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 37 | |
Josh Haberman | 90c7f6e | 2016-04-14 12:48:41 -0700 | [diff] [blame] | 38 | mymessage = MyTestMessage.new(:field1 => 42, :field2 => ["a", "b", "c"]) |
| 39 | mymessage.field1 = 43 |
| 40 | mymessage.field2.push("d") |
| 41 | mymessage.field3 = SubMessage.new(:foo => 100) |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 42 | |
Josh Haberman | 90c7f6e | 2016-04-14 12:48:41 -0700 | [diff] [blame] | 43 | encoded_data = MyTestMessage.encode(mymessage) |
| 44 | decoded = MyTestMessage.decode(encoded_data) |
| 45 | assert decoded == mymessage |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 46 | |
Josh Haberman | 90c7f6e | 2016-04-14 12:48:41 -0700 | [diff] [blame] | 47 | puts "JSON:" |
| 48 | puts MyTestMessage.encode_json(mymessage) |
| 49 | ``` |
Chris Fallin | 21fb217 | 2015-02-23 12:27:52 -0800 | [diff] [blame] | 50 | |
| 51 | Installation from Source (Building Gem) |
| 52 | --------------------------------------- |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 53 | |
| 54 | To build this Ruby extension, you will need: |
| 55 | |
| 56 | * Rake |
| 57 | * Bundler |
| 58 | * Ruby development headers |
| 59 | * a C compiler |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 60 | |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 61 | To Build the JRuby extension, you will need: |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 62 | |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 63 | * Maven |
Adam Greene | d55733c | 2015-05-01 11:54:29 -0700 | [diff] [blame] | 64 | * The latest version of the protobuf java library (see ../java/README.md) |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 65 | * Install JRuby via rbenv or RVM |
| 66 | |
| 67 | First switch to the desired platform with rbenv or RVM. |
| 68 | |
| 69 | Then install the required Ruby gems: |
| 70 | |
| 71 | $ gem install bundler |
| 72 | $ bundle |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 73 | |
| 74 | Then build the Gem: |
| 75 | |
Adam Greene | d55733c | 2015-05-01 11:54:29 -0700 | [diff] [blame] | 76 | $ rake |
| 77 | $ rake clobber_package gem |
Adam Greene | 761cfa0 | 2015-05-01 08:48:56 -0700 | [diff] [blame] | 78 | $ gem install `ls pkg/google-protobuf-*.gem` |
Chris Fallin | 06bf630 | 2015-02-05 14:58:57 -0800 | [diff] [blame] | 79 | |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 80 | To run the specs: |
| 81 | |
| 82 | $ rake test |
| 83 | |
Chris Fallin | 06bf630 | 2015-02-05 14:58:57 -0800 | [diff] [blame] | 84 | This gem includes the upb parsing and serialization library as a single-file |
| 85 | amalgamation. It is up-to-date with upb git commit |
| 86 | `535bc2fe2f2b467f59347ffc9449e11e47791257`. |
Chris Fallin | dfdec3b | 2015-03-03 10:55:55 -0800 | [diff] [blame] | 87 | |
| 88 | Version Number Scheme |
| 89 | --------------------- |
| 90 | |
| 91 | We are using a version number scheme that is a hybrid of Protocol Buffers' |
| 92 | overall version number and some Ruby-specific rules. Gem does not allow |
| 93 | re-uploads of a gem with the same version number, so we add a sequence number |
| 94 | ("upload version") to the version. We also format alphabetical tags (alpha, |
| 95 | pre, ...) slightly differently, and we avoid hyphens. In more detail: |
| 96 | |
| 97 | * First, we determine the prefix: a Protocol Buffers version "3.0.0-alpha-2" |
| 98 | becomes "3.0.0.alpha.2". When we release 3.0.0, this prefix will be simply |
| 99 | "3.0.0". |
| 100 | * We then append the upload version: "3.0.0.alpha.2.0" or "3.0.0.0". If we need |
| 101 | to upload a new version of the gem to fix an issue, the version becomes |
| 102 | "3.0.0.alpha.2.1" or "3.0.0.1". |
| 103 | * If we are working on a prerelease version, we append a prerelease tag: |
| 104 | "3.0.0.alpha.3.0.pre". The prerelease tag comes at the end so that when |
| 105 | version numbers are sorted, any prerelease builds are ordered between the |
| 106 | prior version and current version. |
| 107 | |
| 108 | These rules are designed to work with the sorting rules for |
| 109 | [Gem::Version](http://ruby-doc.org/stdlib-2.0/libdoc/rubygems/rdoc/Gem/Version.html): |
| 110 | release numbers should sort in actual release order. |