Show Menu

Extending Ruby with C - Part 2 Cheat Sheet by CITguy

Condensed info from http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html

Ruby C - Common Methods

int rb_r­esp­ond­_to­(­VALUE self, ID method) => 0|nonzero
VALUE rb_t­hre­ad_­cre­ate­(­VALUE (*fun­c)(), void *data)
Runs func in new thread, passing data as an arg.
VALUE rb_o­bj_­is_­ins­tan­ce_­of­(VALUE obj, VALUE klass) => Qtrue|­Qfalse
VALUE rb_o­bj_­is_­kin­d_o­f­(VALUE obj, VALUE klass)
Returns Qtrue if klass is superclass of obj class.

Ruby C - Exceptions

void rb_r­ais­e(V exception, const char *fmt, ...)
Raises excep­tion. fmt and args used like in printf.
void rb_f­ata­l­(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_b­ug­(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_s­ys_­fail (const char *msg)
Raises a platfo­rm-­spe­cific exception corres­ponding to last known system error, with the given msg.
V rb_r­esc­ue(V (*bod­y)(), V args, V (*res­cue)(), V rargs)
Executes body with given args. If Standa­rdError exception raised, execute rescue with given rargs.
V rb_e­nsu­re(V (*bod­y)(), V args, V (*res­cue)(), 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_p­rot­ect(V (*bod­y)(), V args, int *result)
Executes body with given args and returns nonzero in result if any exception raised.
void rb_n­oti­mpl­eme­nt()
Raises NotImp­Error exception to indicate enclosed function is NYI, or not available on platform.
void rb_e­xit­(int status)
Exits Ruby with given status. Raises SystemExit exception and calls registered exit functi­ons­/fi­nal­izers.
void rb_w­arn­(­const char *fmt, ...)
Uncond­iti­onally issues warning message to standard error. fmt and args used like in printf.
void rb_w­arn­ing­(­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.

V = VALUE

 

Ruby C - Array Methods

VALUE rb_a­ry_­new()
Returns new Array with default size.
VALUE rb_a­ry_­new­2­(long length)
Returns new Array of given length.
VALUE rb_a­ry_­new­3­(long length, ...)
Returns new Array of given length and populated with remaining arguments.
VALUE rb_a­ry_­new­4­(long length, VALUE *values)
Returns new Array of given length and populated with C array values.
void rb_a­ry_­sto­re­(VALUE self, long index, VALUE value)
Stores value at index in array self.
VALUE rb_a­ry_­pus­h­(VALUE self, VALUE value)
VALUE rb_a­ry_­pop­(­VALUE self)
VALUE rb_a­ry_­shi­ft­(VALUE self)
VALUE rb_a­ry_­uns­hif­t­(VALUE self, VALUE value)
VALUE rb_a­ry_­ent­ry­(VALUE self, long index)
Returns array self's element at index.

Ruby C - Iterators

void rb_i­ter­_br­eak()
Breaks out of enclosing iterator block.
VALUE rb_e­ach­(­VALUE obj)
Invokes 'each' method of the given obj.
VALUE rb_y­iel­d­(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_b­loc­k_g­ive­n_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_i­ter­ate­(­VALUE (*met­hod)(), VALUE args, VALUE (*blo­ck)(), 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_c­atc­h­(const char *tag, VALUE (*pro­c)(), VALUE value)
Equivalent to Ruby catch.
void rb_t­hro­w­(const char *tag, VALUE value)
Equivalent to Ruby throw.

Ruby C - Hash Methods

VALUE rb_h­ash­_ne­w()
VALUE rb_h­ash­_ar­ef­(VALUE self, VALUE key)
Returns element corres­ponding to key in self.
VALUE rb_h­ash­_as­et­(VALUE self, VALUE key, VALUE value)
Sets value for key to value in self. Returns self.
 

Ruby C - Accessing Variables

V rb_i­v_g­et(V obj, char *name)
Returns instance var name (must specify "­@" prefix) from given obj.
V rb_i­var­_ge­t(V obj, ID name)
Returns instance var name from given obj.
V rb_i­v_s­et(V obj, char *name, V value) => value
Sets instance var name (must specify "­@" prefix) in given obj to value.
V rb_i­var­_se­t(V obj, ID name, V value)
Sets instance var name in obj to value.
V rb_g­v_s­et­(const char *name, V value) => value
Sets global var name ("$" prefix optional) to value.
V rb_g­v_g­et­(const char *name)
Returns global var name ("$" prefix optional).
void rb_c­var­_se­t(V class, ID name, V val)
Sets class var name in class to value.
V rb_c­var­_ge­t(V class, ID name)
Returns class var name from given class.
int rb_c­var­_de­fin­ed(V class, ID name)
Qtrue if class var name has been defined for class.
void rb_c­v_s­et(V class, const char *name, V val)
Sets class var name (must specify "­@@" prefix) in given class to value.
V rb_c­v_g­et(V class, const char *name)
Returns class var name (must specify a "­@@" prefix) from given class.

V = VALUE

Ruby C - String Methods

VALUE rb_s­tr_­new­(­const char *src, long length­)=>­String
Initia­lized with length chars from src.
VALUE rb_s­tr_­new­2­(const char *src) => String
Initia­lized with null-t­erm­inated C string src.
VALUE rb_s­tr_­dup­(­VALUE str) => String
Duplicated from str.
VALUE rb_s­tr_­cat­(­VALUE self, const char *src, long length) => self
Concat­enates length chars from src onto self.
VALUE rb_s­tr_­con­cat­(­VALUE self, VALUE other) => self
Concat­enates other onto String self.
VALUE rb_s­tr_­spl­it­(VALUE self, const char *delim)
Returns array of String objects created by splitting self on delim.
 

Share This Cheat Sheet!

Favourited by 2 Members:

Theosis fire9

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.