From: Joel VanderWerf Date: 2002-04-27T13:29:50+09:00 Subject: Re: extending ruby: defining method with default parameters value... Baptiste Lepilleur wrote: > rb_define_method( ruby_cScintillaBase, > "sendEditorMessage", > ruby_sendEditorMessage, > 3 ); > > How do I define default value for parameter 2 & 3 ? Is it even possible > to do that from C ? You can use rb_scan_args. See the "Extending Ruby" chapter of the pickaxe book, and the README.EXT that came with your Ruby. Note that you have to use an arg count of -1, so that the args will be passed in a C array, which you pass to rb_scan_args, along with a format string. In the CGenerator library in RAA, this is made even easier. When you define a C function from Ruby, you specify something like c_array_args { required :arg0, :arg1 optional :arg2, :arg3, :arg4 rest :rest block :block } This causes code to be generated using rb_scan_args that parses the arguments into the C variables that you named with the symbols. You can then access them in the body of your C function. You can specify defaults by adding the following to the block above: default :arg3 => "INT2NUM(7)", :arg4 => "INT2NUM(NUM2INT(arg2) + NUM2INT(arg3))" The stuff between the quotes is C code. You can even specify typechecking of your arguments: typecheck :arg2 => Numeric, :arg3 => Numeric See examples/sample.rb for a simple, but complete example.