{"id":7020,"date":"2014-05-17T00:21:09","date_gmt":"2014-05-17T00:21:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/wrapping-c-struct-to-ruby-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:21:09","modified_gmt":"2014-05-17T00:21:09","slug":"wrapping-c-struct-to-ruby-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/wrapping-c-struct-to-ruby-collection-of-common-programming-errors\/","title":{"rendered":"Wrapping C struct to Ruby-Collection of common programming errors"},"content":{"rendered":"<p>How to initialize C Struct and wrap it as Ruby class as parameter of another Ruby object? I am rewriting the memory, but don&#8217;t know, how to fix it.<\/p>\n<p>Ruby code, I want to create instances of Person class and add Address variable into them, which is another class:<\/p>\n<pre><code>require_relative 'my_extension'\n\nclass Address\n  def inspect\n    \"Replaced #inspect: \"\n  end\nend\n\nclass Person\n    def initialize\n        puts \"init\"\n    end\n\n    def print()\n        puts \"Addr class #{@addr.inspect}\"\n    end\nend\n\nfoo1=Person.new\nfoo1.add_address(\"London\")\nfoo1.print\nfoo2=Person.new\nfoo2.add_address(\"Paris\")\nfoo1.print\nfoo2.print\nfoo1.print\n<\/code><\/pre>\n<p>C code, extending Ruby:<\/p>\n<pre><code>#include \n#include \"ruby.h\"\n\nstruct Address {\n    char * town;\n};\n\nstatic VALUE get_addr(VALUE self) {\n    return rb_iv_get(self,\"@addr\");\n}\n\nstatic VALUE wrap_address_get_town(VALUE self) {\n\n    struct Address * address;\n    Data_Get_Struct(self, struct Address, address);\n    return rb_str_new2(address-&gt;town);\n}\n\n\nVALUE foo_class;\nVALUE address_wrapper_class;\n\n\nvoid free_m(){\n    printf(\"free\\n\");\/\/just for test\n}\n\nvoid add_address_t(VALUE self,VALUE new_town){\n    printf(\"add_address\\n\");\n    \/*init new struct and add value to it*\/\n    struct Address addr;\n    addr.town=StringValuePtr(new_town);\n\n    \/*wrap struct*\/\n    VALUE wrapped_address=Data_Wrap_Struct(address_wrapper_class, 0, free_m,&amp;addr);\n\n    \/*set it as instance variable*\/\n    rb_iv_set(self,\"@addr\",wrapped_address);\n}\n\nstatic VALUE foo_class_alloc(VALUE self){\n    return self;\n}\n\n\nvoid Init_my_extension(){\n    foo_class = rb_define_class(\"Person\", rb_cObject);\n\n    address_wrapper_class = rb_define_class(\"Address\", rb_cObject);\n\n    rb_define_method(address_wrapper_class, \"town\", wrap_address_get_town, 0);\n\n    rb_define_method(foo_class, \"add_address\", add_address_t, 1);\n\n}\n<\/code><\/pre>\n<p>Output produces unexpected result:<\/p>\n<pre><code>init\nAddr class Replaced #inspect: \ninit\nAddr class Replaced #inspect:  \/\/London expected\nAddr class Replaced #inspect:  \/\/another problem\nAddr class Replaced #inspect: \nrun\nrun\nfree\nfree\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to initialize C Struct and wrap it as Ruby class as parameter of another Ruby object? I am rewriting the memory, but don&#8217;t know, how to fix it. Ruby code, I want to create instances of Person class and add Address variable into them, which is another class: require_relative &#8216;my_extension&#8217; class Address def inspect [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7020","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7020"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7020\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}