Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created August 22, 2013 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djberg96/6303793 to your computer and use it in GitHub Desktop.
Save djberg96/6303793 to your computer and use it in GitHub Desktop.
// bar.c
#include <ruby.h>
static VALUE bar_init(int argc, VALUE* argv, VALUE self){
VALUE v_func, v_proto, v_return, v_dll, v_array;
int size;
rb_scan_args(argc, argv, "13", &v_func, &v_proto, &v_return, &v_dll);
v_array = rb_str_split(v_proto, "");
// Segfault or bad behavior on Ruby 2.x
if(RARRAY_LEN(v_array) > 5)
rb_raise(rb_eArgError, "too many parameters: %li", RARRAY_LEN(v_array));
size = RARRAY_LEN(v_array);
printf("Size: %li\n", size);
return self;
}
void Init_bar(){
VALUE cBar;
cBar = rb_define_class("Bar", rb_cObject);
rb_define_method(cBar, "initialize", bar_init, -1);
}
# extconf.rb
require 'mkmf'
create_makefile('bar')
# test.rb
$:.unshift Dir.pwd
require 'bar'
Bar.new('test', 's' * 6, 'x', 'whatever')
# Ruby 1.9.x => test.rb:4:in `initialize': too many parameters: 6 (ArgumentError)
# Ruby 2.x => Segfault
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment