Created
April 19, 2012 21:57
-
-
Save mattwildig/2424479 to your computer and use it in GitHub Desktop.
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" | |
struct Address { | |
char * town; | |
}; | |
static VALUE address_alloc(VALUE klass) { | |
struct Address* address = malloc(sizeof(struct Address)); | |
VALUE obj = Data_Wrap_Struct(klass, 0, free, address); | |
return obj; | |
} | |
static VALUE wrap_address_get_town(VALUE self) { | |
struct Address * address; | |
Data_Get_Struct(self, struct Address, address); | |
return rb_str_new2(address->town); | |
} | |
static VALUE address_initialize(VALUE self, VALUE town) { | |
struct Address * address; | |
Data_Get_Struct(self, struct Address, address); | |
address->town = strdup(StringValueCStr(town)); | |
return self; | |
} | |
void Init_my_extension() { | |
VALUE address_wrapper_class = rb_define_class("Address", rb_cObject); | |
rb_define_alloc_func(address_wrapper_class, address_alloc); | |
rb_define_method(address_wrapper_class, "initialize", address_initialize, 1); | |
rb_define_method(address_wrapper_class, "town", wrap_address_get_town, 0); | |
} |
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
require 'mkmf' | |
create_makefile('my_extension') |
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
require './my_extension' | |
class Person | |
attr_accessor :address | |
end | |
person1 = Person.new | |
person1.address = Address.new('London') | |
puts person1.address.town |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment