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 6622857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:27:40+00:00 2026-05-25T21:27:40+00:00

I’m trying to write a C extension to ruby that’ll generate a class. I’m

  • 0

I’m trying to write a C extension to ruby that’ll generate a class. I’m looking on how to define some default arguments to a class. For example, if I have this class decleration in ruby:

class MyClass
  def initialize(name, age=10)
    @name = name
    @age  = age
  end
end

You can initialize it with mc = MyClass.new("blah"), and the age parameter will be set internally. How do I do this in C? So far I got this, but this forces entering the other argument:

require "ruby.h"

static VALUE my_init(VALUE self, VALUE name, VALUE age)
{
    rb_iv_set(self, "@name", name);
    rb_iv_set(self, "@age", age);

    return self;
}

VALUE cMyClass;

void Init_MyClass() 
{
    // create a ruby class instance
    cMyClass = rb_define_class("MyClass", rb_cObject);

    // connect the instance methods to the object
    rb_define_method(cMyClass, "initialize", my_init, 2);
}

I thought about checking the value of age against Qnil or using if ( TYPE(age) == T_UNDEF ), but I just get segfaults from there. Reading through README.EXT leads me to believe I can accomplish this through rb_define_method using the value of argc, but this wasn’t too clear. Any ideas? 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-25T21:27:41+00:00Added an answer on May 25, 2026 at 9:27 pm

    You’re right – you can do this using rb_define_method and a negative value for argc.

    Normally argc specifies the number of arguments your method accepts, but using a negative value specifies that the method accepts a variable number of arguments, which Ruby will pass in as an array.

    There are two possibilities. First, use -1 if you want the arguments passed in to your method in a C array. Your method will have a signature like VALUE func(int argc, VALUE *argv, VALUE obj) where argc is the number of arguments, argv is a pointer to the arguments themselves, and obj is the receiving object, i.e. self. You can then manipulate this array as you need to mimic default arguments or whatever you need, in your case it might look something like this:

    static VALUE my_init(int argc, VALUE* argv, VALUE self) {
    
        VALUE age;
    
        if (argc > 2 || argc == 0) {  // there should only be 1 or 2 arguments
            rb_raise(rb_eArgError, "wrong number of arguments");
        }
    
        rb_iv_set(self, "@name", argv[0]);
    
        if (argc == 2) {        // if age has been included in the call...
            age = argv[1];      // then use the value passed in...
        } else {                // otherwise...
            age = INT2NUM(10);  // use the default value
        }
    
        rb_iv_set(self, "@age", age);
    
        return self;
    }
    

    The alternative is to have a Ruby array passed into your method, which you specify by using -2 in your call to rb_define_method. In this case, your method should have a signature like VALUE func(VALUE obj, VALUE args), where obj is the receiving object (self), and args is a Ruby array containing the arguments. In your case this might look something like this:

    static VALUE my_init(VALUE self, VALUE args) {
    
        VALUE age;
    
        long len = RARRAY_LEN(args);
    
        if (len > 2 || len == 0) {
            rb_raise(rb_eArgError, "wrong number of arguments");
        }
    
        rb_iv_set(self, "@name", rb_ary_entry(args, 0));
    
        if (len == 2) {
            age = rb_ary_entry(args, 1);
        } else {
            age = INT2NUM(10);
        }
    
        rb_iv_set(self, "@age", age);
    
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.