[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Converting nil to NULL in a C extension

djberg96

11/10/2004 6:07:00 PM

Hi all,

I find I'm often doing something like this:

static VALUE foofoo(int argc, VALUE* argv, VALUE self)
char* foo;
VALUE rbFoo;
...

rb_scan_args(argc,argv,"01",&rbFoo);

if(NIL_P(rbFoo)){
foo = NULL;
}
else{
foo = StringValuePtr(rbFoo);
}

some_C_function(foo);
return WHATEVER;
}
/* END */

Is there a way to reduce this to a single step? Can we modify
StringValuePtr() to return NULL if its argument is nil? Or would that
break too many things?

Regards,

Dan
1 Answer

Jamis Buck

11/10/2004 6:23:00 PM

0

Daniel Berger wrote:
> Hi all,
>
> I find I'm often doing something like this:
>
> static VALUE foofoo(int argc, VALUE* argv, VALUE self)
> char* foo;
> VALUE rbFoo;
> ...
>
> rb_scan_args(argc,argv,"01",&rbFoo);
>
> if(NIL_P(rbFoo)){
> foo = NULL;
> }
> else{
> foo = StringValuePtr(rbFoo);
> }

There's always the ternary operator:

foo = NIL_P(rbFoo) ? NULL : StringValuePtr(rbFoo);

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...