Computer Science Canada Writing Ruby in C |
Author: | wtd [ Thu Sep 29, 2005 12:08 pm ] | ||||||||||||
Post subject: | Writing Ruby in C | ||||||||||||
An excruciatingly simple example to demonstrate that it's possible to write Ruby code in C. First off you'll need the Ruby development header files, a C compiler, and a "make" utility... preferably the GNU toolchain. The first of those you should get with the standard Ruby distribution. If you're using a packaged version for a Linux distro, you may have to search harder. When in doubt, compile Ruby from source. Once you've accomplished that, we need to create a C function to wrap. In "function.h":
And in "function.c":
Now, in "myfunc.c" we'll write our Ruby extension.
Now, the compilation isn't hard, but it's not exactly easy, either, so I need some way to create a makefile. Ruby comes to the aid. In file "extconf.rb":
And then we need only run:
Our Ruby extension is now happily sitting there as "myfunc.so". Starting up IRB we can now:
Questions? ![]() |
Author: | Tony [ Thu Sep 29, 2005 12:19 pm ] |
Post subject: | |
and on the flip side Ruby Inline -- writing C in Ruby ![]() |
Author: | wtd [ Thu Sep 29, 2005 12:31 pm ] |
Post subject: | |
I like to think of Ruby's C API as being the Ruby equivalent of assembly. |
Author: | md [ Tue May 30, 2006 10:28 am ] |
Post subject: | |
very nice... Makes me almost want to give it a try ![]() |