extending ruby: defining method with default parameters value... 
Author Message
 extending ruby: defining method with default parameters value...

    Hi everyone,

    the subject said it all: I'm defining a new method in a class:

  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 ?

    Thanks,
    Baptiste.

---

http://www.*-*-*.com/



Wed, 13 Oct 2004 03:59:49 GMT  
 extending ruby: defining method with default parameters value...

Quote:

>   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.



Wed, 13 Oct 2004 20:31:29 GMT  
 extending ruby: defining method with default parameters value...
Quote:
----- Original Message -----


Sent: Saturday, April 27, 2002 6:29 AM
Subject: Re: extending ruby: defining method with default parameters
value...


> >   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.

   Now, I remember reading something about the -1 argument count. Thanks for
pointing it out!

> In the CGenerator library in RAA, this is made even easier. When you
> define a C function from Ruby, you specify something like

   Thanks, I'll give a look to that application. It seems to be an
interesting source of information.

  Baptiste.



Wed, 13 Oct 2004 20:31:49 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Extracting method parameter and default-values?

2. Non-intuitive treatment of empty list as default parameter value on class method

3. Define default value for List Box

4. Default values for named formal parameters

5. Default value on signal parameter of a procedure

6. default values of function parameters (construtors)

7. Why default parameter value not working?

8. Mutable default values for function parameters

9. proc default values for parameters

10. LDV-NCVLOG Displaying values of Verilog define/parameter

11. Possible bug with handling of default values for a method

12. SyntaxError in specify default values for a method's arguments

 

 
Powered by phpBB® Forum Software