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

  • Home
  • SEARCH
  • 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 6331297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:02:10+00:00 2026-05-24T18:02:10+00:00

rb_iv_get and rb_iv_set VS. Data_Wrap_Struct for custom calsses So, reading the Ruby Extension API

  • 0

rb_iv_get and rb_iv_set VS. Data_Wrap_Struct for custom calsses

So, reading the Ruby Extension API I found Data_Wrap_Struct which converts a pointer into a Ruby object (right?). However, when I tried using is I kept getting random errors and whatnot, so I kind of though of switching to something like:

void Init_custom() {
    //cCustom declared as VALUE higher in the code
    cCustom = rb_define_class("Custom", T_OBJECT);
    rb_define_method(mTester, "initialize", init_Custom, 1);
    rb_define_method(mTester, "someValue", someValue_get_Custom, 0);
    rb_define_method(mTester, "someValue=", someValue_set_Custom, 1);
    //And so on.
}

Secondly the someValue_get_Custom and someValue_set_Custom would be:

VALUE someValue_get_Custom(VALUE self) {
    return rb_iv_get(self, "@someValue")
}

VALUE someValue_set_Custom(VALUE self, VALUE val) {
    return rb_iv_set(self, "@someValue", val) //or do I have to do return val;?
}

Another option would be to create a C version of attr_accessor which would make the code slightly shorter and DRYer, but it would still be a part of plan B (using rb_iv_ set/get).


Now, I would really love to figure out how to use Data_Wrap_Struct, since I have a suspicion it might be faster than using rb_iv_set and rb_iv_get, but if it’s not what I think it is, then I’ll have to resort to plan B.

So, I guess the real question is:

What exactly does Data_Wrap_Struct do and how to properly use it or just use it without erroring.


Oh, and I almost forgot, a few more questions related to Data_Wrap_Struct:

What exactly does the mark parameter (a function) do? It says to set the mark parameter to 0 if the object isn’t referenced anywhere, but what if it is referenced somewhere (a RArray maybe)?

Secondly, would the free parameter (also a function) be a simple function that frees the allocated memory for that object?

And is anything passed to those two functions by the Garbage Collector, maybe a VALUE self?

Thanks!

  • 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-05-24T18:02:11+00:00Added an answer on May 24, 2026 at 6:02 pm

    Those are a lot of questions, I’ll try to answer those that I can answer in a few sentences.

    Think of Data_Wrap_Struct as a thin Ruby wrapper that is visible in Ruby as a “normal” object, but actually carries a reference to a C struct (could be anything in theory but just makes sense really for complex types) under its hood. The idea is that this way you can attach a struct to a Ruby object and at any time have access to the struct data whenever Ruby requests it by sending messages to that wrapper object. In C you then retrieve the data from the struct, transform it into something rubyish and pass it back. Or you do the opposite when you are requested to set data in the wrapper object – you transform Ruby data into data that is appropriate for the struct.

    When the garbage collector decides to collect the wrapper object it also wants to free the underlying C struct – this is the job of the free argument, it’s a function pointer to a function that takes the struct type as an argument and ought to free that struct’s memory.

    The mark parameter is a lot more complicated, I’d advise you to post this as a separate question. The good thing: most of the time you won’t need it.

    rb_iv_get, rb_iv_set, as well as rb_ivar_get and rb_ivar_set are for a totally different purpose. They set instance variables of your Ruby object and my guess is this was what you actually were looking for. The point here is that you get/set VALUEs here, i.e. you are working with Ruby objects here instead of plain C types.

    You don’t have to write your C version of attr_accessor, it’s already there: rb_attr.

    To sum this up: Data_Wrap_Struct wraps a container object over your C struct but other than that provides no functionality yet. You should use rb_attr in your Init method if you intend to have attr_accessor functionality. Use rb_iv* if you actually want to set or get instance variables explicitly.`


    I agree that there is a lack of a definite resource on how to write C extensions for Ruby. There are a lot of blog posts, though. From my own experience, I would probably start with the Pickaxe PDF that Serabe mentioned in the comments and there is an actually pretty neat introduction in the Ruby sources (README.EXT). After that the best thing you can do is to look into the extensions in the ext directory in the Ruby sources. Pick one you’re familiar with or whose functionality you like and play with it. Always have the Ruby source code at hand. If you don’t know what a particular function does, look its declaration up in the sources to get at least a feeling for what it does. You don’t need to understand it all at first glance, but it generally gives you enough to move on with your task.

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

Sidebar

Related Questions

I'm using seeds.rb to load some dummy data into my project as I develop
Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception
cqq.rb:96 **warning: Statement not reached.** /root/newpackage/lib.rb:727 **warning: instance variable @object not initialized** Error: Your
ruby somescript.rb somehugelonglistoftextforprocessing is this a bad idea? rather should i create a separate
In my routes.rb file I have the following line: resource :users which gives me
application.rb config.time_zone = 'Athens' I want to list entries which are published between any
environment.rb:7: uninitialized constant Rails::Initializer (NameError) from /usr/lib/ruby/gems/1.8/gems/railties-3.1.0/lib/rails/application.rb:78:in `require_environment!' from /usr/lib/ruby/gems/1.8/gems/railties-3.1.0/lib/rails/commands.rb:39 from script/rails:6:in `require' from
Using redis-rb, how can I push a hash into a list? Do I have
I'm know CruiseControl.rb used for Continuous Integration but it Prerequisites ruby 1.8.6. please sugguest
searchterms.rb search = 1|butter+salted|Butter salt|Butter|Túrós csusza|Margarine|Potato 2|butter+whipped|Shea butter|Butter|Pastry bag|Cream bun|Butter cream 3|butter+oil+anhydrous|Ultralight backpacking|Odell\'s|Ghee|Fragrance extraction|Perfume

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.