[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Defining a method with default arguments in C

Tim Hunter

8/27/2006 8:30:00 PM

Brian Takita wrote:
> Hello,
>
> How can I define a default argument value such as the following in c?
>
> def foobar(arg=1)
> end
>
> Thanks,
> Brian Takita
>
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.

1 Answer

Tim Hunter

8/28/2006 12:37:00 AM

0

Timothy Hunter wrote:
> Brian Takita wrote:
>> Hello,
>>
>> How can I define a default argument value such as the following in c?
>>
>> def foobar(arg=1)
>> end
>>
>> Thanks,
>> Brian Takita
>>
> 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.