ruby fiddle

http://ruby-doc.org/stdlib-2.0.0/libdoc/fiddle/rdoc/Fiddle.html


```rb
require 'fiddle'

libm = Fiddle.dlopen("/usr/lib/x86_64-linux-gnu/libm.so")

floor = Fiddle::Function.new(
  libm['sqrt'],
  [Fiddle::TYPE_DOUBLE],
  Fiddle::TYPE_DOUBLE
)

puts floor.call(81)
```

https://reinteractive.net/posts/261-extending-ruby-with-extensions-ffi-or-inline

```c

#include "ruby.h"

const char lala[] = "just a test";
VALUE tmp;

VALUE hello_ext(VALUE self){
	printf("Hello, extension!\n");
	return Qnil;
}

void Init_helloext(){
  tmp = rb_obj_freeze(rb_str_new2(lala));
	rb_define_global_function("hello_extensions", hello_ext, 0);
	rb_define_global_const("LALA", tmp);
}
```

```c

#include "ruby.h"

const char lala[] = "just a test";
VALUE tmp;

VALUE hello_ext(VALUE self){
	printf("Hello, extension!\n");
	return Qnil;
}

void Init_helloext(){
  tmp = rb_obj_freeze(rb_str_new2(lala));
	rb_define_global_function("hello_extensions", hello_ext, 0);
	rb_define_variable("$lala", &tmp);
}

```

```c
#include "ruby.h"

const char lala[] = "just a test";
VALUE tmp;

VALUE hello_ext(VALUE self){
	printf("Hello, extension!\n");
	return Qnil;
}

void Init_helloext(){
  tmp = rb_obj_freeze(rb_str_new2(lala));
	rb_define_global_function("hello_extensions", hello_ext, 0);
	rb_define_readonly_variable("$lala", &tmp);
}

```

https://blog.jacius.info/ruby-c-extension-cheat-sheet/

```c
#include "ruby.h"

const char lala[] = "just a test";
VALUE tmp;

VALUE hello_ext(VALUE self){
	printf("Hello, extension!\n");
	return Qnil;
}

void Init_helloext(){
  tmp = rb_obj_freeze(rb_str_new2(lala));
	rb_define_global_function("hello_extensions", hello_ext, 0);
	rb_define_const(rb_define_module("RbConfig"),"LALA", tmp);
}
```


```c
#include "ruby.h"

const char lala[] = "just a test";
VALUE tmp;

VALUE hello_ext(VALUE self){
	printf("Hello, extension!\n");
	return Qnil;
}

void Init_helloext(){
  tmp = rb_obj_freeze(rb_str_new2(lala));
	rb_define_global_function("hello_extensions", hello_ext, 0);
	rb_define_const(rb_define_module("RbConfig"),"LALA", tmp);
	rb_define_module("GAGA");
}
```

https://silverhammermba.github.io/emberb/extend/


```rb

#extconf

require 'mkmf'

dir_config('helloext')
create_makefile('lala')
```

```c

#include "ruby.h"

const char lala[] = "just a test";
VALUE tmp;
VALUE m;
VALUE a;

VALUE hello_ext(VALUE self){
	printf("Hello, extension!\n");
	return Qnil;
}

static VALUE
test(int argc, VALUE *argv, VALUE obj)
{
	printf("hahahahaha!\n");
	return Qnil;
}

void Init_lala(){
  tmp = rb_obj_freeze(rb_str_new2(lala));
	rb_define_global_function("hello_extensions", hello_ext, 0);
	rb_define_const(rb_define_module("RbConfig"),"LALA", tmp);
	m = rb_define_class("GAGA",rb_cObject);
	a = rb_define_module_under(m, "HAHA");
  rb_define_method(a, "another", test, -1);
  rb_include_module(m, a);
}

```

```bash
[2] pry(main)> require './lala'
=> true
[3] pry(main)> GAGA
=> GAGA
[4] pry(main)> GAGA.new.another
hahahahaha!
=> nil
```

https://tenderlovemaking.com/2010/12/11/writing-ruby-c-extensions-part-2.html


有必要读一下extension.rdoc


https://wusuopu.gitbooks.io/write-ruby-extension-with-c/content/chapter01/README.html


```bash
vagrant@precise64:~/data/libmagic$ pry
[1] pry(main)> require 'fiddle'
=> true
[2] pry(main)> libm = Fiddle.dlopen('./src/.libs/libmagic.so')
=> #<Fiddle::Handle:0x00000000d20910>
[3] pry(main)> floor = Fiddle::Function.new(
[3] pry(main)*   libm['magic_version'],  
[3] pry(main)*   [Fiddle::TYPE_VOID],  
[3] pry(main)*   Fiddle::TYPE_INT  
[3] pry(main)* )  
=> #<Fiddle::Function:0x00000000a6d3d0 @abi=2, @args=[0], @ptr=139734016460720, @return_type=4>
[4] pry(main)> floor.call
ArgumentError: wrong number of arguments (0 for 1)
from (pry):8:in `call'
[5] pry(main)> floor.call(nil)
=> 518
```