Skip to content

Instantly share code, notes, and snippets.

@matejuh
Created January 19, 2012 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matejuh/1641269 to your computer and use it in GitHub Desktop.
Save matejuh/1641269 to your computer and use it in GitHub Desktop.
Embedding a Ruby Interpreter- working with struct
struct Address {
char * town;
};
struct Person {
struct Address * address;
char * name;
};
static VALUE wrap_person_get_address(VALUE self) {
struct Person * person;
Data_Get_Struct(self, struct Person, person);
VALUE wrapped_address =Data_Wrap_Struct(address_wrapper_class, NULL, NULL, person->address);
return wrapped_address;
}
...
address_wrapper_class = rb_define_class("Address", rb_cObject);
VALUE person_wrapper_class = rb_class_new(rb_cObject);
/*
Add #town and #town= methods to the new class
Here we associate the two implementation functions defined above with the
wrapper class, so that in ruby it will respond to the methods #town and #town=
// */
rb_define_method(address_wrapper_class, "town", wrap_address_get_town, 0); //getter method: #town
rb_define_method(address_wrapper_class, "town=", wrap_address_set_town, 1);//setter method: #town=
rb_define_method(person_wrapper_class, "name", wrap_person_get_name, 0);
rb_define_method(person_wrapper_class, "name=", wrap_person_set_name, 1);
rb_define_method(person_wrapper_class, "address", wrap_person_get_address,
0);
...
/*
============================================================================
Name : rub_ext_test.c
Author : Matej
Version :
Copyright :
Description : Working version with struct defined and inicialized by Ruby C API
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "ruby.h"
static int id_print_method;
static VALUE klass;
struct Address {
char * town;
};
static VALUE wrap_print(VALUE args) {
VALUE *values = (VALUE *) args;
VALUE testClass = values[0];
VALUE adr = values[1];
return rb_funcall(testClass, id_print_method, 1, adr);
}
static VALUE protected_print(VALUE testClass, VALUE adr) {
int error;
VALUE args[2];
VALUE result;
args[0] = testClass;
args[1] = adr;
result = rb_protect(wrap_print, (VALUE) args, &error);
return error ? Qnil : result;
}
int main(int argc, char **argv) {
int error;
struct Address adr;
adr.town = "London";
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
ruby_script("demo_embedder"); /* sets name in error messages */
rb_protect((VALUE(*)(VALUE)) rb_require, (VALUE) "test", &error);
VALUE testClass = rb_class_new_instance(0, 0, rb_const_get(rb_cObject,
rb_intern("TestKlass")));
VALUE adr_ptr;
adr_ptr = Data_Wrap_Struct(klass,0,0,&adr);
VALUE adr_struct = rb_struct_define("Adresa","town" , 0);
VALUE inst_adr_struct=rb_struct_new(adr_struct,1);
rb_struct_aset(inst_adr_struct,rb_str_new2("town"),rb_str_new2("London"));
id_print_method = rb_intern("print_info");
// VALUE result;
// VALUE result = protected_print(testClass, adr_ptr);
VALUE result = protected_print(testClass, inst_adr_struct);
if (NIL_P(result))
printf("Nil!\n");
else
printf("Returned %d\n", NUM2INT(result));
puts("FINISHED"); /* prints FINISHED */
return ruby_cleanup(0);
}
/*
============================================================================
Name : rub_ext_test.c
Author : Matej
Version :
Copyright :
Description : Wrapping struct (not working)
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "ruby.h"
static int id_print_method;
static VALUE klass;
struct Address {
char * town;
};
static VALUE wrap_print(VALUE args) {
VALUE *values = (VALUE *) args;
VALUE testClass = values[0];
VALUE adr = values[1];
return rb_funcall(testClass, id_print_method, 1, adr);
}
static VALUE protected_print(VALUE testClass, VALUE adr) {
int error;
VALUE args[2];
VALUE result;
args[0] = testClass;
args[1] = adr;
result = rb_protect(wrap_print, (VALUE)args, &error);
return error ? Qnil : result;
}
int main(int argc, char **argv) {
int error;
struct Address adr;
adr.town = "London";
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
ruby_script("demo_embedder"); /* sets name in error messages */
rb_protect((VALUE(*)(VALUE)) rb_require, (VALUE) "test", &error);
VALUE testClass = rb_class_new_instance(0, 0, rb_const_get(rb_cObject,
rb_intern("TestKlass")));
VALUE adr_ptr;
adr_ptr = Data_Wrap_Struct(klass,0,0,&adr);
id_print_method = rb_intern("print_info");
VALUE result = protected_print(testClass, adr_ptr);
if (NIL_P(result))
printf("Nil!\n");
else
printf("Returned %d\n", NUM2INT(result));
puts("FINISHED"); /* prints FINISHED */
return ruby_cleanup(0);
}
require 'test'
class TestKlass
def print_info(struct)
puts "In Ruby"
puts struct.inspect
puts struct.town
return 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment