Ok, quindi mi sono seduto un mio amico che è bravo con C. Gli ho mostrato Ruby e lui lo scava. Quando ci siamo incontrati ieri sera gli ho detto che potresti scrivere gemme di Ruby in C, cosa che lo ha intrigato. Ecco cosa abbiamo trovato:
Tutorial/Esempi
http://www.eqqon.com/index.php/Ruby_C_Extension
http://drnicwilliams.com/2008/04/01/writing-c-extensions-in-rubygems/
http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
ruby-doc (codice sorgente ruby.h)
http://ruby-doc.org/doxygen/1.8.4/ruby_8h-source.html
Ecco alcuni codice sorgente che abbiamo scritto di provarlo così:
aprire un terminale:
prompt>mkdir MyTest
prompt>cd MyTest
prompt>gedit extconf.rb
Poi si mette questo codice in extconf.rb
# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'
# Give it a name
extension_name = 'mytest'
# The destination
dir_config(extension_name)
# Do the work
create_makefile(extension_name)
Salva il file quindi scrive MyTest.c
#include "ruby.h"
// Defining a space for information and references about the module to be stored internally
VALUE MyTest = Qnil;
// Prototype for the initialization method - Ruby calls this, not you
void Init_mytest();
// Prototype for our method 'test1' - methods are prefixed by 'method_' here
VALUE method_test1(VALUE self);
VALUE method_add(VALUE, VALUE, VALUE);
// The initialization method for this module
void Init_mytest() {
MyTest = rb_define_module("MyTest");
rb_define_method(MyTest, "test1", method_test1, 0);
rb_define_method(MyTest, "add", method_add, 2);
}
// Our 'test1' method.. it simply returns a value of '10' for now.
VALUE method_test1(VALUE self) {
int x = 10;
return INT2NUM(x);
}
// This is the method we added to test out passing parameters
VALUE method_add(VALUE self, VALUE first, VALUE second) {
int a = NUM2INT(first);
int b = NUM2INT(second);
return INT2NUM(a + b);
}
Dal prompt è quindi necessario creare un Makefile per extconf.rb eseguendo:
prompt>ruby extconf.rb
prompt>make
prompt>make install
È quindi possibile verificare il lavoro svolto:
prompt>irb
irb>require 'mytest'
irb>include MyTest
irb>add 3, 4 # => 7
abbiamo fatto un test di benchmark e aveva rubino aggiungere 3 e 4 insieme 10 milioni di volte e poi fare una chiamata a la nostra estensione C 10 milioni di volte. Il risultato è stato che usando solo il rubino ci sono voluti 12 secondi per completare questa operazione mentre l'estensione C richiedeva solo 6 secondi! Si noti inoltre che la maggior parte di questa elaborazione sta trasferendo il lavoro a C per completare l'attività. In uno di questi tutorial lo scrittore ha usato la ricorsione (sequenza di Fibonacci) e ha riferito che l'estensione C ha preso 51 volte più velocemente!
Suggerirei di trovare un progetto open source come RMagick o Nokogiri e la culla da quello. –