GT.M binding to Ruby


GT.M binding to Ruby



M is both a language and a database. Just like with most other popular NoSQL databases it is useful to be able to access M from many different languages, even though using it from the M language might be the preferred approach. Bindings to other languages make the M database available to a larger number of applications and facilitates the growth of a larger community.


A Ruby module has now been created to make possible to use GT.M from Ruby scripts.

This Ruby module builds upon the gtm_cip C-API of GT.M.

It wraps the C API into a C++ class, that in turn is wrapped with SWIG for Ruby.


Details of this Ruby binding for GT.M are available in this article in the OSEHRA Technical Journal.


Note that this provides only the GT.M bindings for Ruby.
It is expected that GT.M will already be installed in the target system.


After installing the gtm Ruby module, you still have to proceed with the two following post-installation steps


A) Set the GTMCI environment variable to the full path of the file.

B) Copy the _gtmaccess.m file in to one of the directories where you have M routines. These directories are the ones that you already include in the gtmroutines environment variable.

C) Add to your LD_LIBRARY_PATH environment variable the directory where you installed GT.M.

More precisely, the directory where the GT.M library


is currently located. This will make possible for the Ruby binding to find the GT.M shared library when you run Ruby scripts. This directory is what you would typically have in the "gtm_dist" environment variable.



Once you have completed the post-installation, you can try some of the tests available at


For example, start Ruby code such as:


require "./gtm"

db =

print db.version()

print db.about()


Then follow with more interesting code, such as:


require "./gtm"

db =

globalName = '^Capital("US")'

setValue = 'Washington'

db.set( globalName, setValue )

globalName = '^Capital("UK")'

setValue = 'London'

db.set( globalName, setValue )

getValue = db.order( globalName )

puts "Order of ", globalName, " = ", getValue

getValue = db.query( globalName )

puts "Query of ", globalName, " = ", getValue

db.execute( 'write $ZVERSION,!')




Enjoy GT.M from Ruby !




This has now been packaged as

Luis Ibanez's picture

This has now been packaged as a Ruby Gem.

You can find it at:


The Github repository has been updated accordingly:

You should now be able to install it by simply typing:

                    gem   install   gtm

Note that this command will only install the bindings for gtm.

You must still install GT.M the usual way.

Please let us know if you find any issues with this Gem package.


Thanks to Tyler Kiley for his help and guidance on how to create a Gem package.