Converting nil to NULL in a C extension

D

Daniel Berger

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
 
J

Jamis Buck

Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top