좋아, 그래서 나는 C와 잘 어울리는 친구를 앉았다. 나는 루비를 보여 주었고 그는 그것을 파다. 어젯밤에 만났을 때 루비 보석을 C에 쓸 수 있다고 말했고, 그 점이 그를 흥미롭게 만들었습니다. 여기에서 우리가 발견 한 것입니다 :
자습서/예
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.h 소스 코드)
http://ruby-doc.org/doxygen/1.8.4/ruby_8h-source.html
터미널 열고 :여기
우리가뿐만 아니라 그것을 테스트하기 위해 쓴 일부 소스 코드
prompt>mkdir MyTest
prompt>cd MyTest
prompt>gedit extconf.rb
는 그런 다음 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)
저장 파일은 다음 다음을 만드는 데 필요한 프롬프트에서 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);
}
쓰기
prompt>ruby extconf.rb
prompt>make
prompt>make install
당신은 다음을 테스트 할 수 있습니다 :
prompt>irb
irb>require 'mytest'
irb>include MyTest
irb>add 3, 4 # => 7
우리는 벤치 마크 테스트를했고, 루비가 3, 4 함께 1000 만 회를 추가 한 다음에 전화를 걸 가지고 실행 extconf.rb하여 메이크 우리의 C 확장자는 1 천만 번입니다. 그 결과 루비 만 사용하면 C 확장을 사용하는 데 6 초 밖에 걸리지 않고이 작업을 완료하는 데 12 초가 걸렸습니다! 또한이 처리의 대부분은 작업을 완료하기 위해 C로 작업을 넘겨주는 것입니다. 이 자습서 중 하나에서 필자는 재귀 (피보나치 시퀀스)를 사용하여 C 확장이 51 배 빨랐다고보고했습니다!
나는 RMagick이나 Nokogiri와 같은 오픈 소스 프로젝트를 찾는 것이 좋습니다. –