Show Menu

Extending Ruby with C - Part 2 Cheat Sheet by

Condensed info from

Ruby C - Common Methods

int rb_res­pond_to(VALUE self, ID method) => 0|nonzero
VALUE rb_thr­ead­_create(VALUE (*func)(), void *data)
Runs func in new thread, passing data as an arg.
VALUE rb_obj­_is­_in­sta­nce_of(VALUE obj, VALUE klass) => Qtrue|­Qfalse
VALUE rb_obj­_is­_ki­nd_of(VALUE obj, VALUE klass)
Returns Qtrue if klass is superclass of obj class.

Ruby C - Exceptions

void rb_raise(V exception, const char *fmt, ...)
Raises exception. fmt and args used like in printf.
void rb_fatal(const char *fmt, ...)
Raises Fatal exception, termin­ating process. No rescue blocks called, but ensure blocks will be called. fmt and args used like in printf.
void rb_bug(const char *fmt, ...)
Terminates process immedi­ate­ly--no handlers of any sort called. fmt and args are interp­reted like printf. Call only if a fatal bug has been exposed.
void rb_sys­_fail (const char *msg)
Raises a platfo­rm-­spe­cific exception corres­ponding to last known system error, with the given msg.
V rb_rescue(V (*body)(), V args, V (*resc­ue)(), V rargs)
Executes body with given args. If Standa­rdError exception raised, execute rescue with given rargs.
V rb_ensure(V (*body)(), V args, V (*resc­ue)(), V eargs)
Executes body with given args. Whether or not an exception is raised, execute ensure with given rargs after body has completed.
V rb_protect(V (*body)(), V args, int *result)
Executes body with given args and returns nonzero in result if any exception raised.
void rb_not­imp­lement()
Raises NotImp­Error exception to indicate enclosed function is NYI, or not available on platform.
void rb_exit(int status)
Exits Ruby with given status. Raises SystemExit exception and calls registered exit functi­ons­/fi­nal­izers.
void rb_warn(const char *fmt, ...)
Uncond­iti­onally issues warning message to standard error. fmt and args used like in printf.
void rb_warning(const char *fmt, ...)
Condit­ionally issues a warning message to standard error if Ruby was invoked with the -w flag. fmt and args used like in printf.

Ruby C - Array Methods

VALUE rb_ary_new()
Returns new Array with default size.
VALUE rb_ary­_new2(long length)
Returns new Array of given length.
VALUE rb_ary­_new3(long length, ...)
Returns new Array of given length and populated with remaining arguments.
VALUE rb_ary­_new4(long length, VALUE *values)
Returns new Array of given length and populated with C array values.
void rb_ary­_store(VALUE self, long index, VALUE value)
Stores value at index in array self.
VALUE rb_ary­_push(VALUE self, VALUE value)
VALUE rb_ary_pop(VALUE self)
VALUE rb_ary­_shift(VALUE self)
VALUE rb_ary­_un­shift(VALUE self, VALUE value)
VALUE rb_ary­_entry(VALUE self, long index)
Returns array self's element at index.

Ruby C - Iterators

void rb_ite­r_break()
Breaks out of enclosing iterator block.
VALUE rb_each(VALUE obj)
Invokes 'each' method of the given obj.
VALUE rb_yield(VALUE arg)
Transfers execution to iterator block in the current context, passing arg as an argument. Multiple values may be passed in an array.
int rb_blo­ck_­given_p()
Nonzero if yield would execute a block in current contex­t--that is, if a code block was passed to current method and is available to be called.
VALUE rb_iterate(VALUE (*meth­od)(), VALUE args, VALUE (*bloc­k)(), VALUE arg2)
Invokes method with args and block block. Yield from that method will invoke block with arg given to yield and second arg arg2.
VALUE rb_catch(const char *tag, VALUE (*proc)(), VALUE value)
Equivalent to Ruby catch.
void rb_throw(const char *tag, VALUE value)
Equivalent to Ruby throw.

Ruby C - Hash Methods

VALUE rb_has­h_new()
VALUE rb_has­h_aref(VALUE self, VALUE key)
Returns element corres­ponding to key in self.
VALUE rb_has­h_aset(VALUE self, VALUE key, VALUE value)
Sets value for key to value in self. Returns self.

Ruby C - Accessing Variables

V rb_iv_get(V obj, char *name)
Returns instance var name (must specify "­@" prefix) from given obj.
V rb_iva­r_get(V obj, ID name)
Returns instance var name from given obj.
V rb_iv_set(V obj, char *name, V value) => value
Sets instance var name (must specify "­@" prefix) in given obj to value.
V rb_iva­r_set(V obj, ID name, V value)
Sets instance var name in obj to value.
V rb_gv_set(const char *name, V value) => value
Sets global var name ("$" prefix optional) to value.
V rb_gv_get(const char *name)
Returns global var name ("$" prefix optional).
void rb_cva­r_set(V class, ID name, V val)
Sets class var name in class to value.
V rb_cva­r_get(V class, ID name)
Returns class var name from given class.
int rb_cva­r_d­efined(V class, ID name)
Qtrue if class var name has been defined for class.
void rb_cv_set(V class, const char *name, V val)
Sets class var name (must specify "­@@" prefix) in given class to value.
V rb_cv_get(V class, const char *name)
Returns class var name (must specify a "­@@" prefix) from given class.

Ruby C - String Methods

VALUE rb_str_new(const char *src, long length­)=>­String
Initia­lized with length chars from src.
VALUE rb_str­_new2(const char *src) => String
Initia­lized with null-t­erm­inated C string src.
VALUE rb_str_dup(VALUE str) => String
Duplicated from str.
VALUE rb_str_cat(VALUE self, const char *src, long length) => self
Concat­enates length chars from src onto self.
VALUE rb_str­_concat(VALUE self, VALUE other) => self
Concat­enates other onto String self.
VALUE rb_str­_split(VALUE self, const char *delim)
Returns array of String objects created by splitting self on delim.


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Extending Ruby with C - Part 1 Cheat Sheet
            Ruby on Rails Cheat Sheet by Dave Child and

          More Cheat Sheets by CITguy

          jasmine JS testing Cheat Sheet
          Extending Ruby with C - Part 1 Cheat Sheet
          *nix users and groups Cheat Sheet