Compile Ruby with the C API

If I take the ruby ​​code

puts "Hello World!" 

and rewrite it using the Ruby C API

 #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 Ruby code and rewrite it as C, can I call it a “Ruby compiler”? Is there some ruby ​​code that cannot be rewritten in Ruby this way? Has anyone tried to write this "compiler" before?

+7
source share
2 answers

Some good articles on this subject:

Also have you heard of Crystal ? Although not true Ruby, it looks interesting:

Crystal is a programming language with the following objectives:

  • Have the same syntax as Ruby, or at least as similar as possible.
  • You never need to specify the type of an argument to a variable or method.
  • Ability to call C code by writing bindings to it in Crystal.
  • Be able to compile time and generate code to avoid template code.
  • Compile efficient native code.

about it on SO: Has anyone tried Crystal programming language (compiled using Ruby machine code)?

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

+4
source

yes, this is somehow the ruby ​​code of the c-ified code.

The closest things to "ruby to c" were http://ruby2cext.rubyforge.org , rubinius with its JIT compiler and ruby2c

http://betterlogic.com/roger/2009/08/how-to-use-the-ruby2c-gem

Another option is to write a JIT compiler for 1.9 bytecode, which can speed things up a bit.

Also see mirah, which looks like static compiles ruby ​​time.

Theoretically, this should be possible.

+1
source

All Articles