Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. # extconf.rb
  2. require 'mkmf'
  3. create_makefile('foo')
  4.  
  5. # foo.h
  6. #ifndef __FOO_H_INCLUDED__
  7. #define __FOO_H_INCLUDED__
  8.  
  9. #include <ruby.h>
  10.  
  11. extern VALUE cFoo;
  12. extern VALUE cBar;
  13.  
  14. static VALUE rb_hash_aref2(VALUE v_hash, char* key){
  15. VALUE v_key, v_val;
  16.  
  17. v_key = rb_str_new2(key);
  18. v_val = rb_hash_aref(v_hash, v_key);
  19.  
  20. if(NIL_P(v_val))
  21. v_val = rb_hash_aref(v_hash, ID2SYM(rb_intern(key)));
  22.  
  23. return v_val;
  24. }
  25.  
  26. #endif
  27.  
  28. // foo.c
  29. #include <foo.h>
  30.  
  31. static VALUE foo_alpha(VALUE v_hash){
  32. VALUE v_hello = rb_hash_aref2(v_hash, "hello");
  33. return v_hello;
  34. }
  35.  
  36. void Init_foo(){
  37. cFoo = rb_define_class("Foo", rb_cObject);
  38.  
  39. rb_define_method(cFoo, "alpha", foo_alpha, 1);
  40.  
  41. Init_bar();
  42. }
  43.  
  44. // bar.c
  45. #include <foo.h>
  46.  
  47. void Init_bar(){
  48. cBar = rb_define_class("Bar", rb_cObject);
  49. }
Add Comment
Please, Sign In to add comment