<commit_before>.. http://docs.mongodb.org/ecosystem/tutorial/ruby-bson-tutorial/

.. _ruby-bson-tutorial:

=================
BSON 3.x Tutorial
=================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: twocols

This tutorial discusses using the core Ruby BSON gem.

Installation
------------

The BSON gem is hosted on `Rubygems <http://rubygems.org>`_ and can be installed
manually or with bundler.

To install the gem manually:

.. code-block:: sh

    gem install bson

To install the gem with bundler, include the following in your Gemfile:

.. code-block:: ruby

    gem 'bson', '~> 3.0'

The BSON gem is compatible with MRI 1.9.3, 2.0.x, 2.1.x, 2.2.x, JRuby 1.7.x, and
Rubinius 2.5.x


BSON Serialization
------------------

Getting a Ruby object's raw BSON representation is done by calling ``to_bson``
on the Ruby object. For example:

.. code-block:: ruby

  "Shall I compare thee to a summer's day".to_bson
  1024.to_bson

Generating an object from BSON is done via calling ``from_bson`` on the class
you wish to instantiate and passing it the ``StringIO`` bytes.

.. code-block:: ruby

  String.from_bson(string_io)
  Int32.from_bson(string_io)
<commit_msg>Adjust bson v3 docs to reference v3 (#221)<commit_after>.. http://docs.mongodb.org/ecosystem/tutorial/ruby-bson-tutorial/

.. _ruby-bson-tutorial:

=================
BSON 3.x Tutorial
=================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: twocols

This tutorial discusses using the core Ruby BSON gem.

Installation
------------

The BSON library is hosted on `Rubygems <http://rubygems.org>`_ and can be
installed manually or with bundler.

To install the gem manually:

.. code-block:: sh

    gem install bson -v '~> 3.0'

To install the gem with bundler, include the following in your ``Gemfile``:

.. code-block:: ruby

    gem 'bson', '~> 3.0'

The BSON gem is compatible with MRI 1.9.3, 2.0.x, 2.1.x, 2.2.x, JRuby 1.7.x,
and Rubinius 2.5.x


BSON Serialization
------------------

Getting a Ruby object's raw BSON representation is done by calling ``to_bson``
on the Ruby object. For example:

.. code-block:: ruby

  "Shall I compare thee to a summer's day".to_bson
  1024.to_bson

Generating an object from BSON is done via calling ``from_bson`` on the class
you wish to instantiate and passing it the ``StringIO`` bytes.

.. code-block:: ruby

  String.from_bson(string_io)
  Int32.from_bson(string_io)

{'fraction_non_alphanumeric': 0.15393747526711515, 'fraction_numerical': 0.020973486347447567, 'mean_word_length': 3.178512396694215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1973761', 'n_tokens_mistral': 943, 'n_tokens_neox': 848, 'n_words': 301}