22
33.. _ruby-bson-tutorial-4-0:
44
5- ================= 
5+ ***************** 
66BSON 4.x Tutorial
7- ================= 
7+ ***************** 
88
99.. default-domain:: mongodb
1010
@@ -17,7 +17,7 @@ BSON 4.x Tutorial
1717This tutorial discusses using the Ruby BSON library.
1818
1919Installation
20- ------------ 
20+ ============ 
2121
2222The BSON library can be installed from `Rubygems <http://rubygems.org>`_
2323manually or with bundler.
@@ -37,7 +37,7 @@ To install the gem with bundler, include the following in your ``Gemfile``:
3737The BSON library is compatible with MRI >= 2.3 and JRuby >= 9.2.
3838
3939Use With ActiveSupport
40- ---------------------- 
40+ ====================== 
4141
4242Serialization for ActiveSupport-defined classes, such as TimeWithZone, is
4343not loaded by default to avoid a hard dependency of BSON on ActiveSupport.
@@ -50,7 +50,7 @@ ActiveSupport-related code must be explicitly required:
5050    require 'bson/active_support'
5151
5252BSON Serialization
53- ------------------ 
53+ ================== 
5454
5555Getting a Ruby object's raw BSON representation is done by calling ``to_bson``
5656on the Ruby object, which will return a ``BSON::ByteBuffer``. For example:
@@ -70,13 +70,13 @@ you wish to instantiate and passing it a ``BSON::ByteBuffer`` instance.
7070
7171
7272Byte Buffers
73- ------------ 
73+ ============ 
7474
7575BSON library 4.0 introduces the use of native byte buffers in MRI and JRuby
7676instead of using ``StringIO``, for improved performance.
7777
7878Writing
79- ``````` 
79+ ------- 
8080
8181To create a ``ByteBuffer`` for writing (i.e. serializing to BSON),
8282instantiate ``BSON::ByteBuffer`` with no arguments:
@@ -184,7 +184,7 @@ over a socket), call ``to_s`` on the buffer:
184184
185185
186186Reading
187- ``````` 
187+ ------- 
188188
189189To create a ``ByteBuffer`` for reading (i.e. deserializing from BSON),
190190instantiate ``BSON::ByteBuffer`` with a byte string as the argument:
@@ -218,7 +218,7 @@ To restart reading from the beginning of a buffer, use ``rewind``:
218218
219219
220220Supported Classes
221- ----------------- 
221+ ================= 
222222
223223Core Ruby classes that have representations in the BSON specification and
224224will have a ``to_bson`` method defined for them are: ``Object``, ``Array``,
@@ -230,7 +230,7 @@ specific to the specification:
230230
231231
232232``BSON::Binary``
233- ```````````````` 
233+ ---------------- 
234234
235235Use ``BSON::Binary`` objects to store arbitrary binary data. The ``Binary``
236236objects can be constructed from binary strings as follows:
@@ -281,7 +281,7 @@ The data and the subtype can be retrieved from ``Binary`` instances using
281281    # => #<Encoding:ASCII-8BIT>
282282
283283UUID Methods
284- ~~~~~~~~~~~~ 
284+ ```````````` 
285285
286286To create a UUID BSON::Binary (binary subtype 4) from its RFC 4122-compliant
287287string representation, use the ``from_uuid`` method:
@@ -314,7 +314,7 @@ Note that the ``:standard`` representation can only be used with a Binary
314314of subtype ``:uuid`` (not ``:uuid_old``).
315315
316316Legacy UUIDs
317- ~~~~~~~~~~~~ 
317+ ```````````` 
318318
319319Data stored in BSON::Binary objects of subtype 3 (``:uuid_old``) may be
320320persisted in one of three different byte orders depending on which driver
@@ -381,7 +381,7 @@ These methods can be used to convert from one representation to another:
381381
382382
383383``BSON::Code``
384- `````````````` 
384+ -------------- 
385385
386386Represents a string of JavaScript code.
387387
@@ -390,7 +390,7 @@ Represents a string of JavaScript code.
390390  BSON::Code.new("this.value = 5;")
391391
392392``BSON::CodeWithScope``
393- ``````````````````````` 
393+ ----------------------- 
394394
395395.. note::
396396
@@ -406,7 +406,7 @@ Represents a string of JavaScript code with a hash of values.
406406  BSON::CodeWithScope.new("this.value = age;", age: 5)
407407
408408``BSON::Document``
409- `````````````````` 
409+ ------------------ 
410410
411411This is a subclass of ``Hash`` that stores all keys as strings, but allows
412412access to them with symbol keys.
@@ -431,7 +431,7 @@ access to them with symbol keys.
431431
432432
433433``BSON::MaxKey``
434- ```````````````` 
434+ ---------------- 
435435
436436Represents a value in BSON that will always compare higher to another value.
437437
@@ -440,7 +440,7 @@ Represents a value in BSON that will always compare higher to another value.
440440  BSON::MaxKey.new
441441
442442``BSON::MinKey``
443- ```````````````` 
443+ ---------------- 
444444
445445Represents a value in BSON that will always compare lower to another value.
446446
@@ -449,7 +449,7 @@ Represents a value in BSON that will always compare lower to another value.
449449  BSON::MinKey.new
450450
451451``BSON::ObjectId``
452- `````````````````` 
452+ ------------------ 
453453
454454Represents a 12 byte unique identifier for an object on a given machine.
455455
@@ -458,7 +458,7 @@ Represents a 12 byte unique identifier for an object on a given machine.
458458  BSON::ObjectId.new
459459
460460``BSON::Timestamp``
461- ``````````````````` 
461+ ------------------- 
462462
463463Represents a special time with a start and increment value.
464464
@@ -467,7 +467,7 @@ Represents a special time with a start and increment value.
467467  BSON::Timestamp.new(5, 30)
468468
469469``BSON::Undefined``
470- ``````````````````` 
470+ ------------------- 
471471
472472Represents a placeholder for a value that was not provided.
473473
@@ -476,7 +476,7 @@ Represents a placeholder for a value that was not provided.
476476  BSON::Undefined.new
477477
478478``BSON::Decimal128``
479- ```````````````````` 
479+ -------------------- 
480480
481481Represents a 128-bit decimal-based floating-point value capable of emulating
482482decimal rounding with exact precision.
@@ -491,7 +491,7 @@ decimal rounding with exact precision.
491491  BSON::Decimal128.new(d)
492492
493493``Symbol``
494- `````````` 
494+ ---------- 
495495
496496The BSON specification defines a symbol type which allows round-tripping
497497Ruby ``Symbol`` values (i.e., a Ruby ``Symbol``is encoded into a BSON symbol
@@ -538,7 +538,7 @@ To force encoding of Ruby symbols to BSON symbols, wrap the Ruby symbols in
538538  # => "\x12\x00\x00\x00\x0Efoo\x00\x04\x00\x00\x00bar\x00\x00"
539539
540540JSON Serialization
541- ------------------ 
541+ ================== 
542542
543543Some BSON types have special representations in JSON. These are as follows
544544and will be automatically serialized in the form when calling ``to_json`` on
@@ -577,7 +577,7 @@ them.
577577
578578
579579Time Instances
580- -------------- 
580+ ============== 
581581
582582Times in Ruby can have nanosecond precision. Times in BSON (and MongoDB)
583583can only have millisecond precision. When Ruby ``Time`` instances are
@@ -610,7 +610,7 @@ calculations may produce unexpected results.
610610
611611
612612DateTime Instances
613- ------------------ 
613+ ================== 
614614
615615BSON only supports storing the time as the number of seconds since the
616616Unix epoch. Ruby's ``DateTime`` instances can be serialized to BSON,
@@ -624,7 +624,7 @@ database.
624624
625625
626626Date Instances
627- -------------- 
627+ ============== 
628628
629629BSON only supports storing the time as the number of seconds since the
630630Unix epoch. Ruby's ``Date`` instances can be serialized to BSON, but when
@@ -635,7 +635,7 @@ of the day that the ``Date`` refers to in UTC.
635635
636636
637637Regular Expressions
638- ------------------- 
638+ =================== 
639639
640640Ruby regular expressions always have BSON regular expressions' equivalent of
641641'm' flag on. In order for behavior to be preserved between the two, the 'm'
@@ -683,7 +683,7 @@ object, one must call ``compile`` on the returned object.
683683
684684
685685Key Order
686- --------- 
686+ ========= 
687687
688688BSON documents preserve the order of keys, because the documents are stored
689689as lists of key-value pairs. Hashes in Ruby also preserve key order; thus
@@ -693,7 +693,7 @@ the order of keys in the document will match the order of keys in the hash.
693693
694694
695695Duplicate Keys
696- -------------- 
696+ ============== 
697697
698698BSON specification allows BSON documents to have duplicate keys, because the
699699documents are stored as lists of key-value pairs. Applications should refrain
0 commit comments