Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7812543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:32:35+00:00 2026-06-02T04:32:35+00:00

How to initialize C Struct and wrap it as Ruby class as parameter of

  • 0

How to initialize C Struct and wrap it as Ruby class as parameter of another Ruby object? I am rewriting the memory, but don’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 'my_extension'

class Address
  def inspect
    "Replaced #inspect: <Address: town:#{town}>"
  end
end

class Person
    def initialize
        puts "init"
    end

    def print()
        puts "Addr class #{@addr.inspect}"
    end
end

foo1=Person.new
foo1.add_address("London")
foo1.print
foo2=Person.new
foo2.add_address("Paris")
foo1.print
foo2.print
foo1.print

C code, extending Ruby:

#include <stdio.h>
#include "ruby.h"

struct Address {
    char * town;
};

static VALUE get_addr(VALUE self) {
    return rb_iv_get(self,"@addr");
}

static VALUE wrap_address_get_town(VALUE self) {

    struct Address * address;
    Data_Get_Struct(self, struct Address, address);
    return rb_str_new2(address->town);
}


VALUE foo_class;
VALUE address_wrapper_class;


void free_m(){
    printf("free\n");//just for test
}

void add_address_t(VALUE self,VALUE new_town){
    printf("add_address\n");
    /*init new struct and add value to it*/
    struct Address addr;
    addr.town=StringValuePtr(new_town);

    /*wrap struct*/
    VALUE wrapped_address=Data_Wrap_Struct(address_wrapper_class, 0, free_m,&addr);

    /*set it as instance variable*/
    rb_iv_set(self,"@addr",wrapped_address);
}

static VALUE foo_class_alloc(VALUE self){
    return self;
}


void Init_my_extension(){
    foo_class = rb_define_class("Person", rb_cObject);

    address_wrapper_class = rb_define_class("Address", rb_cObject);

    rb_define_method(address_wrapper_class, "town", wrap_address_get_town, 0);

    rb_define_method(foo_class, "add_address", add_address_t, 1);

}

Output produces unexpected result:

init
Addr class Replaced #inspect: <Address: town:London>
init
Addr class Replaced #inspect: <Address: town:Paris> //London expected
Addr class Replaced #inspect: <Address: town:�)> //another problem
Addr class Replaced #inspect: <Address: town:�)>
run
run
free
free
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:32:37+00:00Added an answer on June 2, 2026 at 4:32 am

    All the C routines should be defined as static returning a VALUE. This should be Qnil, if nothing relevant should be returned. There is also another problem, with the add_address_t routine – you are wrapping a locally defined struct – it should be allocated with ALLOC(struct Address);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to initialize the structure in another class? there is struct in first class:
I am trying to initialize the following array of the following struct, but my
Code: struct IRenderingEngine { virtual void Initialize(int width, int height) = 0; virtual void
I'm not sure how to initialize a class property that's a C struct, or
Is there possible to initialize a object-table of a struct in c before main?
I want to initialize an array of struct, however the second parameter of memset()
I have some code to initialize a struct in C#: namespace Practice { public
I am actually trying to initialize vectors in constructors of struct/class. I came up
I have a struct that I initialize like this: typedef struct { word w;
When initialize an entity framework context. One is to initialize at class level, such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.