Show Menu
Cheatography

Extending Ruby with C - Part 1 Cheat Sheet by

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

Ruby C - Define Objects

V rb_def­ine­_class(char *name, V superc­lass)
Defines new class at top level with given name and superclass.
V rb_def­ine­_module(char *name)
Defines new module at top level with given name.
V rb_def­ine­_cl­ass­_under(V under, char *name, V superc­lass)
Defines nested class under class or module under.
V rb_def­ine­_mo­dul­e_under(V parent, V module)
Defines nested module under class or module under.
void rb_inc­lud­e_m­odule(V parent, V module)
Includes given module into class or module parent.
void rb_ext­end­_object(V obj, V module)
Extends obj with module.
V rb_require(const char *name)
Equiv. to "­require *name."­ Returns Qtrue or Qfalse.
V = VALUE

Ruby C - Calling Methods

V rb_funcall(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_fun­call2(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_fun­call3(V recv, ID id, int argc, V *args)
Same as rb_fun­call2, but will not call private methods.
V rb_apply(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_intern(char *name)
Returns ID for given name. If name does not exist, a symbol table entry will be created for it.
char * rb_id2name(ID id)
Returns a name for the given id.
V rb_cal­l_super(int argc, V *args)
Calls current method in superclass of current object.
V = VALUE

Ruby C - Object Status

OBJ_TAINT(VALUE obj)
OBJ_FREEZE(VALUE obj)
int OBJ_TA­INTED(VALUE obj) => 0|nonzero
int OBJ_FROZEN(VALUE obj) => 0|nonzero
 

Ruby C - Defining Variables and Constants

void rb_def­ine­_const(VALUE classmod, char *name, VALUE value)
Defines constant in class or module classmod, with given name and value.
void rb_def­ine­_gl­oba­l_const(char *name, VALUE value)
Defines global constant with given name and value.
void rb_def­ine­_va­riable(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_def­ine­_cl­ass­_va­riable(VALUE class, const char *name, VALUE val)
Defines class variable name (must specify "­@@" prefix) in given class, initia­lized to value.
void rb_def­ine­_vi­rtu­al_­var­iable(const char *name, VALUE (*gett­er)(), void (*sett­er)())
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_def­ine­_ho­oke­d_v­ariable(const cahr *name, VALUE *variable, VALUE (*gett­er)(), void (*sett­er)())
Defines functions to be called when readin­g/w­riting to variable. (See also rb_def­ine­_vi­rtu­al_­var­iable.)
void rb_def­ine­_re­ado­nly­_va­riable(const char *name, VALUE *value)
Same as rb_def­ine­_va­riable, but read-only from Ruby.
void rb_def­ine­_attr(VALUE variable, const char *name, int read, int write)
Creates accessor methods for given variable, with given name. If read is nonzero, crate read method; if write is nonzero, create write method.
void rb_glo­bal­_va­riable(VALUE *obj)
Registers given address with garbage collector.

Ruby C - Security Status

Check_­SafeStr(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_saf­e_level()
void rb_secure(int level)
Raises Securi­tyError if level <= current safe level.
void rb_set­_sa­fe_­level(int newlevel)
 

Ruby C - Defining Methods

void rb_def­ine­_method(V classmod, char *name, V (*func)(), 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 Prototype)
void rb_def­ine­_mo­dul­e_f­unction(V classmod, char *name, V (*func)(), int argc);
Defines method in class classmod with given name, implem­ented by C function func taking argc args.
void rb_def­ine­_gl­oba­l_f­unction(char *name, V (*func)(), int argc);
Defines global function (private Kernel method) with given name, implem­ented by C function func and taking argc args.
void rb_def­ine­_si­ngl­eto­n_m­ethod(V classmod, char *name, V (*func)(), int argc);
Defines singleton method in class classmod with given name, implem­ented by C function func taking argc args.
int rb_sca­n_args(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_und­ef_­method(V classmod, const char *name);
Undefines method name in class or module classmod.
void rb_def­ine­_alias(V classmod, const char *newname, const char *oldname);
Defines alias for oldname in class or module classmod.
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.
               
 

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