Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Ruby with its C API

If I take a ruby code

puts "Hello World!"

and rewrite it using the C API of Ruby

#include "ruby.h"

int main() {
  ruby_init();

  rb_funcall(Qnil, rb_intern("puts"), 1, rb_str_new2("Hello World!"));

  ruby_finalize();
  return 0;
}

and compile it, is this a way to compile Ruby code?

If I create a program that uses Ripper to parse the Ruby code and rewrite it as C, can I call it as a "Ruby compiler"? There're some ruby code that can't be rewrited in Ruby in this way? Did someone tried to write this kind of "compiler" before?

like image 247
Guilherme Bernal Avatar asked May 23 '11 23:05

Guilherme Bernal


1 Answers

A couple of good articles on this topic:

  • http://whitequark.org/blog/2011/12/21/statically-compiled-ruby/
  • a bit dated... http://macournoyer.com/blog/2008/12/09/orange/

Also have you heard of Crystal? While not truly Ruby, it looks interesting:

Crystal is a programming language with the following goals:

  • Have the same syntax as Ruby, or at least as similar as possible.
  • Never have to specify the type of a variable or method argument.
  • Be able to call C code by writing bindings to it in Crystal.
  • Have compile-time evaluation and generation of code, to avoid boilerplate code.
  • Compile to efficient native code.

about it on SO: Anybody tried the Crystal Programming Language (machine-code compiled Ruby)?

And another (commercial) project with the same purposes but mainly targeted at embedded development: http://foundry-lang.org/

like image 111
JfredoJ Avatar answered Sep 18 '22 04:09

JfredoJ