Show Menu

Extending Ruby with C - Part 1 Cheat Sheet by

Condensed from http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
development     ruby     c     extending     basics

Ruby C - Define Objects

V rb_d­efi­ne_­cla­ss­(char *name, V superc­lass)
Defines new class at top level with given name and super­class.
V rb_d­efi­ne_­mod­ule­(char *name)
Defines new module at top level with given name.
V rb_d­efi­ne_­cla­ss_­und­er(V under, char *name, V superc­lass)
Defines nested class under class or module under.
V rb_d­efi­ne_­mod­ule­_un­der(V parent, V module)
Defines nested module under class or module under.
void rb_i­ncl­ude­_mo­dul­e(V parent, V module)
Includes given module into class or module parent.
void rb_e­xte­nd_­obj­ect(V obj, V module)
Extends obj with module.
V rb_r­equ­ire­(­const char *name)
Equiv. to "­require *name."­ Returns Qtrue or Qfalse.
V = VALUE

Ruby C - Calling Methods

V rb_f­unc­all(V recv, ID id, int argc, ...)
Invokes method given by id in object recv with given number of args argc and args themse­lves.
V rb_f­unc­all­2(V recv, ID id, int argc, V *args)
Invokes method given by id in object recv with given number of args argc and args themselves given in C array args.
V rb_f­unc­all­3(V recv, ID id, int argc, V *args)
Same as rb_fu­nca­ll2, but will not call private methods.
V rb_a­ppl­y(V recv, ID name, int argc, V args)
Invokes method given by id in object recv with given number of args argc and the args themselves given in Ruby Array args.
ID rb_i­nte­rn­(char *name)
Returns ID for given name. If name does not exist, a symbol table entry will be created for it.
char * rb_i­d2n­ame­(ID id)
Returns a name for the given id.
V rb_c­all­_su­per­(int argc, V *args)
Calls current method in superclass of current object.
V = VALUE

Ruby C - Object Status

OBJ_­TAI­NT­(VALUE obj)
OBJ_­FRE­EZE­(­VALUE obj)
int OBJ_­TAI­NTE­D­(VALUE obj) => 0|nonzero
int OBJ_­FRO­ZEN­(­VALUE obj) => 0|nonzero
 

Ruby C - Defining Variables and Constants

void rb_d­efi­ne_­con­st­(VALUE classmod, char *name, VALUE value)
Defines constant in class or module class­mod, with given name and value.
void rb_d­efi­ne_­glo­bal­_co­nst­(char *name, VALUE value)
Defines global constant with given name and value.
void rb_d­efi­ne_­var­iab­le­(const char *name, VALUE *object)
Exports address of given object that was created in C, to the Ruby namespace as name. To Ruby, this will be a global variable, so name should have "­$" prefix. Be sure to honor Ruby's rules for allowed variable names.
void rb_d­efi­ne_­cla­ss_­var­iab­le­(VALUE class, const char *name, VALUE val)
Defines class variable name (must specify "­@@" prefix) in given class, initia­lized to value.
void rb_d­efi­ne_­vir­tua­l_v­ari­abl­e­(const char *name, VALUE (*get­ter)(), void (*set­ter)())
Exports virtual variable to Ruby namespace as global $name. No actual storage exists for variable; attempts to get/set value will call the approp­riate functions.
void rb_d­efi­ne_­hoo­ked­_va­ria­ble­(­const cahr *name, VALUE *vari­able, VALUE (*get­ter)(), void (*set­ter)())
Defines functions to be called when readin­g/w­riting to varia­ble. (See also rb_d­efi­ne_­vir­tua­l_v­ari­abl­e.)
void rb_d­efi­ne_­rea­don­ly_­var­iab­le­(const char *name, VALUE *value)
Same as rb_def­ine­_va­riable, but read-only from Ruby.
void rb_d­efi­ne_­att­r­(VALUE variable, const char *name, int read, int write)
Creates accessor methods for given varia­ble, with given name. If read is nonzero, crate read method; if write is nonzero, create write method.
void rb_g­lob­al_­var­iab­le­(VALUE *obj)
Registers given address with garbage collector.

Ruby C - Security Status

Chec­k_S­afe­Str­(­VALUE str)
Raises Securi­tyError if current safe level > 0 and str is tainted, or a TypeError if str is not a T_STRING.
int rb_s­afe­_le­vel()
void rb_s­ecu­re­(int level)
Raises Securi­tyError if level <= current safe level.
void rb_s­et_­saf­e_l­eve­l(int newlevel)
 

Ruby C - Defining Methods

void rb_d­efi­ne_­met­hod(V classmod, char *name, V (*fun­c)(), int argc);
Defines instance method in class or module classmod with given name, implem­ented by C function func and taking argc args. (See Ruby C - Function Protot­ype)
void rb_d­efi­ne_­mod­ule­_fu­nct­ion(V classmod, char *name, V (*fun­c)(), int argc);
Defines method in class classmod with given name, implem­ented by C function func taking argc args.
void rb_d­efi­ne_­glo­bal­_fu­nct­ion­(char *name, V (*fun­c)(), int argc);
Defines global function (private Kernel method) with given name, implem­ented by C function func and taking argc args.
void rb_d­efi­ne_­sin­gle­ton­_me­tho­d(V classmod, char *name, V (*fun­c)(), int argc);
Defines singleton method in class classmod with given name, implem­ented by C function func taking argc args.
int rb_s­can­_ar­gs­(int argc, V *argv, char *fmt, ...)
Scans argument list and assigns to variables similar to scanf: fmt is string containing zero, one, or two digits followed by optional flag chars. First char indicates count of mandatory args; second is count of optional args. A "­*" means to pack remaining args into Ruby array. A "­&" means attached code block will be taken and assigned to given variable (Qnil will be assigned if no code block given). After fmt string, pointers to VALUE are given to which args are assigned.
void rb_u­nde­f_m­eth­od(V classmod, const char *name);
Undefines method name in class or module class­mod.
void rb_d­efi­ne_­ali­as(V classmod, const char *newname, const char *oldn­ame);
Defines alias for oldname in class or module class­mod.
V = VALUE

Ruby C - Function Prototype

(argc) 0..17 VALUE func(­VALUE self, VALUE arg...)
C function will be called with this many arguments.
(argc) -1 VALUE func(int argc, VALUE *argv, VALUE self)
C function will be given a variable number of arguments passed as a C array.
(argc) -2 VALUE func(­VALUE self, VALUE args)
C function will be given a variable number of arguments passed as a Ruby array.

Download the Extending Ruby with C - Part 1 Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/citguy_extending-ruby-with-c-part-1.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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.

          Related Cheat Sheets

          Extending Ruby with C - Part 2 Cheat Sheet
            Ruby on Rails Cheat Sheet by Dave Child and addedbytes.com

          More Cheat Sheets by CITguy

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