Skip to content

Instantly share code, notes, and snippets.

@banister
Created October 10, 2010 07:05
Show Gist options
  • Save banister/619040 to your computer and use it in GitHub Desktop.
Save banister/619040 to your computer and use it in GitHub Desktop.
/* (c) 2010 John Mair (banisterfiend), MIT license */
#include "compat.h"
#include "ruby.h"
VALUE
rb_prepend_module(VALUE klass, VALUE module)
{
VALUE m = rb_module_new();
st_free_table(RCLASS_M_TBL(m));
RCLASS_M_TBL(m) = RCLASS_M_TBL(klass);
/* create module, and set its M_TBL to a copy of the klass's (we'll include this module later) */
RCLASS_M_TBL(klass) = st_init_numtable();
rb_funcall(klass, rb_intern("include"), 1, m);
rb_funcall(klass, rb_intern("include"), 1, module);
rb_clear_cache();
return klass;
}
void
Init_prepend() {
rb_define_method(rb_cModule, "prepend", rb_prepend_module, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment