Can't Have More Than 9 Mandatory/Optional Arguments in Ruby/C?

Hi,

In rb_scan_args() the char *fmt is specified as a string containing zero,
one, or two digits followed by some flag characters. Does this plainly
mean that we cannot have more than 9 mandatory arguments?

At first glance it seems that we can have more than 9 optional arguments
by simply packing the rest of arguments into array, but this will not
limit how many arguments a user can specify. The same is true by using
the “VALUE func (VALUE self, VALUE args)” format instead of the “VALUE
func (int argc, VALUE *argv, VALUE self)” format. I prefer the later
because, combined with rb_scan_args(), we can specify how many mandatory
and optional arguments, which can be used to trap user’s errors in using a
method. (Well, we can also test RARRAY(args)->len, but it means it is
more work.)

Then, does this design mean that:

“VALUE func (VALUE self, VALUR args)” is the more general format, but it
requires more work for the C programmer. “VALUE func (int argc, VALUE
*argv, VALUE self)” is more like a shortcut, but it comes with some limits
on the number of arguments.

? Or is there any other design philosophy behind these two function
formats?

Regards,

Bill

Well, you could change the definition of isdigit(), and then you can get
as many arguments as you want. :slight_smile:

Or, you can scan the arguments yourself. There’s no written rule that
says you have to use rb_scan_args(); it’s only there for convenience.

Paul

···

On Mon, Aug 19, 2002 at 11:28:24PM +0900, William Djaja Tjokroaminata wrote:

In rb_scan_args() the char *fmt is specified as a string containing zero,
one, or two digits followed by some flag characters. Does this plainly
mean that we cannot have more than 9 mandatory arguments?

Hi,

In rb_scan_args() the char *fmt is specified as a string containing zero,
one, or two digits followed by some flag characters. Does this plainly
mean that we cannot have more than 9 mandatory arguments?

By using rb_scan_args(), yes. For that case (I believe it is very
rare case), you can receive arguments via C argc/argv style (-1 for
rb_define_method) or Ruby args style (-2), then check the number of
argument by yourself.

Then, does this design mean that:

“VALUE func (VALUE self, VALUR args)” is the more general format, but it
requires more work for the C programmer. “VALUE func (int argc, VALUE
*argv, VALUE self)” is more like a shortcut, but it comes with some limits
on the number of arguments.

? Or is there any other design philosophy behind these two function
formats?

I needed both style in the implementation of the Ruby class library.
So I made both to reduce my effort.

						matz.
···

In message “Can’t Have More Than 9 Mandatory/Optional Arguments in Ruby/C?” on 02/08/19, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:

Hi Paul,

I have read the definition of rb_scan_args() and probably I will not dare
to mess with changing ISDIGIT(); I think it will be really tricky. And
for scanning the arguments themselves, you are perfectly right, it is
there for convenience; it is really inconvenient to write our own scanning
arguments.

Actually there is a deeper and wider question. For example, in this case,
we can use either the -1-arity format or the -2-arity format. How do you
choose among the two? Do you consistently use only the -2-arity format
all the time?

The deeper and wider question is, has there been any published good
guidelines in writing Ruby-C extension? It seems that even the Ruby C API
provides several possible ways of doing things. Will it be beneficial if
someone tries to compile all the known good practice and possible pitfalls
in combining Ruby and C? Currently my guess is, a good knowledge of C
by itself and a good knowledge or Ruby by itself is still not 100%
adequate for writing a good Ruby-C combination; they need to be
complemented with this additional knowledge.

Regards,

Bill

···

=============================================================================
Paul Brannan pbrannan@atdesk.com wrote:

On Mon, Aug 19, 2002 at 11:28:24PM +0900, William Djaja Tjokroaminata wrote:

In rb_scan_args() the char *fmt is specified as a string containing zero,
one, or two digits followed by some flag characters. Does this plainly
mean that we cannot have more than 9 mandatory arguments?

Well, you could change the definition of isdigit(), and then you can get
as many arguments as you want. :slight_smile:

Or, you can scan the arguments yourself. There’s no written rule that
says you have to use rb_scan_args(); it’s only there for convenience.

Paul