Created
June 28, 2011 17:35
-
-
Save superplussed/1051678 to your computer and use it in GitHub Desktop.
Testing ary functions in C extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ruby.h" | |
#include "stdlib.h" | |
VALUE TestArray; | |
VALUE add(VALUE self, VALUE val, VALUE index) { | |
VALUE rAry; | |
rAry = rb_ivar_get(self, rb_intern("@rb_ary")); | |
VALUE *ary_ptr = RARRAY_PTR(rAry); | |
ary_ptr[NUM2INT(index)] = val; | |
return ary_ptr[NUM2INT(index)]; | |
} | |
VALUE all (VALUE self, VALUE index) { | |
int i; | |
VALUE rAry; | |
rAry = rb_ivar_get(self, rb_intern("@rb_ary")); | |
for (i = 0; i <= NUM2INT(index); i++) { | |
printf("num: %d\n", NUM2INT(RARRAY_PTR(rAry)[i])); | |
} | |
return Qnil; | |
} | |
VALUE get(VALUE self, VALUE index) { | |
VALUE rAry; | |
rAry = rb_ivar_get(self, rb_intern("@rb_ary")); | |
return RARRAY_PTR(rAry)[NUM2INT(index)]; | |
} | |
VALUE initialize_test_class(VALUE self) { | |
VALUE rAry; | |
rAry = rb_ary_new2(8); | |
rb_define_variable("@rb_ary", 0); | |
rb_iv_set(self, "@rb_ary", rAry); | |
} | |
void Init_ary(VALUE self){ | |
TestArray = rb_define_class("TestArray", rb_cObject); | |
rb_define_method(TestArray, "add", add, 2); | |
rb_define_method(TestArray, "all", all, 1); | |
rb_define_method(TestArray, "get", get, 1); | |
rb_define_method(TestArray, "initialize", initialize_test_class, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment