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