Saturday, May 24, 2008

Ruby 1.9 C API - Data Conversion

Ruby -> C


char* RSTRING_PTR(VALUE); /* Ruby String to char* */
int RSTRING_LEN(VALUE); /* String length */
char* StringValuePtr(VALUE); /* Returns string representation of any object */
RSTRING StringValue(VALUE); /* Returns Ruby string representation of any object */
int NUM2INT(VALUE); /* Number to Int */
double NUM2DBL(VALUE); /* Double to Int */
VALUE* RARRAY_PTR(RARRAY); /*Returns the pointer to values of a Ruby Array */
int RARRAY_LEN(RARRAY); /* Returns length of Array */


C -> Ruby

/* Integer to Ruby Number*/
INT2NUM(number);
UINT2NUM(number);
DOUBLE2NUM(double); /* Or rb_float_new(double) */
RSTRING rb_str_new(char*, len); /* Returns ruby string from char* and length*/
RSTRING rb_str_new2(char*); /* Returns ruby string from null terminated char* */


Generating Arrays

VALUE array = rb_ary_new(); /* Create the array */
rb_ary_push(array, VALUE); /* Push data into the array */


Generating a Hash

VALUE hash = rb_hash_new();
rb_hash_aset(hash, VALUE key, VALUE);


Get the type of a Ruby VALUE object
rb_type(VALUE);
Possible Types:

#define T_NONE RUBY_T_NONE
#define T_NIL RUBY_T_NIL
#define T_OBJECT RUBY_T_OBJECT
#define T_CLASS RUBY_T_CLASS
#define T_ICLASS RUBY_T_ICLASS
#define T_MODULE RUBY_T_MODULE
#define T_FLOAT RUBY_T_FLOAT
#define T_STRING RUBY_T_STRING
#define T_REGEXP RUBY_T_REGEXP
#define T_ARRAY RUBY_T_ARRAY
#define T_HASH RUBY_T_HASH
#define T_STRUCT RUBY_T_STRUCT
#define T_BIGNUM RUBY_T_BIGNUM
#define T_FILE RUBY_T_FILE
#define T_FIXNUM RUBY_T_FIXNUM
#define T_TRUE RUBY_T_TRUE
#define T_FALSE RUBY_T_FALSE
#define T_DATA RUBY_T_DATA
#define T_MATCH RUBY_T_MATCH
#define T_SYMBOL RUBY_T_SYMBOL
#define T_RATIONAL RUBY_T_RATIONAL
#define T_COMPLEX RUBY_T_COMPLEX
#define T_VALUES RUBY_T_VALUES
#define T_UNDEF RUBY_T_UNDEF
#define T_NODE RUBY_T_NODE
#define T_MASK RUBY_T_MASK


Iterate through a Ruby Hash
We iterate with the function
rb_hash_foreach(hash, iterator_c_function, extra_value);
iterator_c_function is of the form:
int iterator(VALUE key, VALUE value, VALUE extra);
Remeber VALUE is the same size as a void*, this means we can typecast it into whatever we want.
To tell the rb_ruby_foreach() function to continue we return a ST_CONTINUE in the iterator function. We can also return a ST_STOP, ST_DELETE, or ST_CHECK.

No comments: