blob: 78e86015d763723360aa83fd8ed67334501a1118 [file] [log] [blame] [view]
Chris Fallin973f4252014-11-18 14:19:58 -08001This directory contains the Ruby extension that implements Protocol Buffers
2functionality in Ruby.
3
4The Ruby extension makes use of generated Ruby code that defines message and
5enum types in a Ruby DSL. You may write definitions in this DSL directly, but
6we recommend using protoc's Ruby generation support with .proto files. The
7build process in this directory only installs the extension; you need to
8install protoc as well to have Ruby code generation functionality.
9
Chris Fallin21fb2172015-02-23 12:27:52 -080010Installation from Gem
11---------------------
12
13When 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
15install it as you would any other gem:
16
17 $ gem install [--prerelease] google-protobuf
18
Chris Fallin21fb2172015-02-23 12:27:52 -080019Once the gem is installed, you may or may not need `protoc`. If you write your
20message type descriptions directly in the Ruby DSL, you do not need it.
21However, if you wish to generate the Ruby DSL from a `.proto` file, you will
22also want to install Protocol Buffers itself, as described in this repository's
23main `README` file. The version of `protoc` included in the latest release
24supports the `--ruby_out` option to generate Ruby code.
25
26A simple example of using the Ruby extension follows. More extensive
27documentation may be found in the RubyDoc comments (`call-seq` tags) in the
28source, and we plan to release separate, more detailed, documentation at a
29later date.
30
Josh Haberman90c7f6e2016-04-14 12:48:41 -070031```ruby
32require 'google/protobuf'
Chris Fallin21fb2172015-02-23 12:27:52 -080033
Josh Haberman90c7f6e2016-04-14 12:48:41 -070034# generated from my_proto_types.proto with protoc:
35# $ protoc --ruby_out=. my_proto_types.proto
36require 'my_proto_types'
Chris Fallin21fb2172015-02-23 12:27:52 -080037
Josh Haberman90c7f6e2016-04-14 12:48:41 -070038mymessage = MyTestMessage.new(:field1 => 42, :field2 => ["a", "b", "c"])
39mymessage.field1 = 43
40mymessage.field2.push("d")
41mymessage.field3 = SubMessage.new(:foo => 100)
Chris Fallin21fb2172015-02-23 12:27:52 -080042
Josh Haberman90c7f6e2016-04-14 12:48:41 -070043encoded_data = MyTestMessage.encode(mymessage)
44decoded = MyTestMessage.decode(encoded_data)
45assert decoded == mymessage
Chris Fallin21fb2172015-02-23 12:27:52 -080046
Josh Haberman90c7f6e2016-04-14 12:48:41 -070047puts "JSON:"
48puts MyTestMessage.encode_json(mymessage)
49```
Chris Fallin21fb2172015-02-23 12:27:52 -080050
51Installation from Source (Building Gem)
52---------------------------------------
Chris Fallin973f4252014-11-18 14:19:58 -080053
54To build this Ruby extension, you will need:
55
56* Rake
57* Bundler
58* Ruby development headers
59* a C compiler
Chris Fallin973f4252014-11-18 14:19:58 -080060
Isaiah Peng27e2b572014-12-24 15:48:41 +010061To Build the JRuby extension, you will need:
Chris Fallin973f4252014-11-18 14:19:58 -080062
Isaiah Peng27e2b572014-12-24 15:48:41 +010063* Maven
Adam Greened55733c2015-05-01 11:54:29 -070064* The latest version of the protobuf java library (see ../java/README.md)
Isaiah Peng27e2b572014-12-24 15:48:41 +010065* Install JRuby via rbenv or RVM
66
67First switch to the desired platform with rbenv or RVM.
68
69Then install the required Ruby gems:
70
71 $ gem install bundler
72 $ bundle
Chris Fallin973f4252014-11-18 14:19:58 -080073
74Then build the Gem:
75
Adam Greened55733c2015-05-01 11:54:29 -070076 $ rake
77 $ rake clobber_package gem
Adam Greene761cfa02015-05-01 08:48:56 -070078 $ gem install `ls pkg/google-protobuf-*.gem`
Chris Fallin06bf6302015-02-05 14:58:57 -080079
Isaiah Peng27e2b572014-12-24 15:48:41 +010080To run the specs:
81
82 $ rake test
83
Chris Fallin06bf6302015-02-05 14:58:57 -080084This gem includes the upb parsing and serialization library as a single-file
85amalgamation. It is up-to-date with upb git commit
86`535bc2fe2f2b467f59347ffc9449e11e47791257`.
Chris Fallindfdec3b2015-03-03 10:55:55 -080087
88Version Number Scheme
89---------------------
90
91We are using a version number scheme that is a hybrid of Protocol Buffers'
92overall version number and some Ruby-specific rules. Gem does not allow
93re-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,
95pre, ...) 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
108These 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):
110release numbers should sort in actual release order.