Defining a method with default arguments in C

Hello,

How can I define a default argument value such as the following in c?

def foobar(arg=1)
end

Thanks,
Brian T.

Brian T. wrote:

Hello,

How can I define a default argument value such as the following in c?

def foobar(arg=1)
end

Thanks,
Brian T.

Specify the number of arguments as -1 in rb_define_method:

rb_define_method(myclass, “method_name”, meth_fnc, -1);

In your method use rb_scan_args to scan the argument list. If you don’t
get enough arguments, use the default values instead.

Timothy H. wrote:

Specify the number of arguments as -1 in rb_define_method:

rb_define_method(myclass, “method_name”, meth_fnc, -1);

In your method use rb_scan_args to scan the argument list. If you
don’t get enough arguments, use the default values instead.

My earlier post was incomplete. I should’ve explained that you should
define your method like this:

VALUE meth_fnc(int argc, VALUE *argv, VALUE self)
{
// stuff
}

argv is a C array of VALUEs, one for each argument passed to the method.
argc is the number of VALUEs in argv.

rb_scan_args takes argc and argv as arguments.