SciRuby/rubex

Name: rubex

Owner: SciRuby

Description: rubex - A Ruby-like language for writing Ruby C extensions.

Created: 2016-08-10 04:47:19.0

Updated: 2018-05-22 17:12:54.0

Pushed: 2018-05-22 17:12:52.0

Homepage:

Size: 1998

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Rubex

Rubex is a Ruby-like language for writing Ruby C extensions.

Rubex is a language that makes writing CRuby C extensions as simple as writing Ruby. It does this by providing a syntax that is the perfect blend of the elegance of Ruby and the power of C. Rubex compiles to C and implicitly interfaces with the Ruby VM in a manner that is completely transparent to the programmer.

Rubex keeps you happy even when writing C extensions.

Status

Gem Version Open Source Helpers Build Status

Table of Contents

Quick Introduction

Consider this Ruby code for computing a fibonnaci series and returning it in an Array:

s Fibonnaci
f compute(n)
i = 1, prev = 1, current = 1, temp
arr = []

while i < n do
  temp = current
  current = current + prev
  prev = temp
  arr.push(prev)
  i += 1
end

arr
d

If you decide to port this to a C extension, the code will look like so:

lude <ruby.h>
lude <stdint.h>

 Init_a ();
ic VALUE Fibonnaci_compute (int argc,VALUE* argv,VALUE self);

ic VALUE Fibonnaci_compute (int argc,VALUE* argv,VALUE self)

t n,i,prev,current,temp;
LUE arr;

 (argc < 1) {
rb_raise(rb_eArgError, "Need 1 args, not %d", argc);


      = NUM2INT(argv[0]);
      = 1;
ev    = 1;
rrent = 1;
r     = rb_ary_new2(0);

ile (i < n)

temp = current;
current = current + prev;
prev = temp;
rb_funcall(arr, rb_intern("push"), 1 ,INT2NUM(prev));
i = i + 1;


turn arr;


 Init_a ()

LUE cls_Fibonnaci;

s_Fibonnaci = rb_define_class("Fibonnaci", rb_cObject);

_define_method(cls_Fibonnaci ,"compute", Fibonnaci_compute, -1);

However, if you decide to write a C extension using Rubex, the code will look like this!:

s Fibonnaci
f compute(int n)
int i = 1, prev = 1, current = 1, temp
array = []

while i < n do
  temp = current
  current = current + prev
  prev = temp
  array.push(prev)
  i += 1
end

return array
d

Notice the only difference between the above Rubex code and Ruby is the specification of explicit int types for the variables. Above Rubex code will automatically compile into C code and will also implicitly interface with the Ruby VM without you having to remember any of the APIs.

Rubex also takes care of the initial setup and compilation of the C files, so all you need to do is execute a bunch of commands and your extension is up and running!

Installation

Requires Ruby version >= 2.3.0

Install with:

install rubex

Usage

Installing the gem will also install the rubex binary. You can now write a Rubex file (with a .rubex file extension) and compile it into C code using the following commands.

Commands
Generate

Create all the necessary files for the C extension.

x generate file_name.rubex

ons
, [--force]            # replace existing files and directories
, [--dir=DIR]          # specify a directory for generating files
, [--install]          # automatically run install command after generating Makefile
, [--debug]            # enable debugging symbols when compiling with GCC
Install

Run the make utility on generated files.

x install path/to/generated/directory
Help

Describe available commands or one specific command

x help [COMMAND]
Manual Usage

If you want to manually generate the files, you can do that with:

x file_name.rubex

This will produce the translated C code and an extconf.rb file inside a directory called file_name. CD into the directory, and run the extconf.rb file with:

 extconf.rb

This will produce a Makefile. Run make to compile the generated C file and generate a .so shared object file that can be used in any Ruby script.

Tutorial

Give yourself 5 min and go through the TUTORIAL. Convert a part of your C extension to Rubex and see the jump in cleanliness and productivity for yourself.

Syntax

Read the full Rubex reference in REFERENCE.

Differences with Ruby

Although Rubex tries its best to support the Ruby syntax as much as possible, in some cases it is not feasible or necessary to provide full support. Following is a list of differences between Ruby and Rubex syntax:

Roadmap

See the CONTRIBUTING and the GitHub issue tracker for future features.

Acknowledgements


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.