Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created October 2, 2016 19:54
Show Gist options
  • Save mooreniemi/0cebeb756b23f6debea3de380721b406 to your computer and use it in GitHub Desktop.
Save mooreniemi/0cebeb756b23f6debea3de380721b406 to your computer and use it in GitHub Desktop.
/*
* =====================================================================================
*
* Filename: Compose.c
*
* Description: Proc composition
*
* Version: 1.0
* Created: 10/01/2016 16:14:22
* Revision: none
* Compiler: gcc
*
* Author: Alex Moore-Niemi
*
* =====================================================================================
*/
#include <ruby.h>
// Defining a space for information and references about the module to be stored internally
VALUE Compose = Qnil;
// Prototype for the initialization method - Ruby calls this, not you
void Init_compose();
VALUE compose(VALUE dummy, VALUE args, int argc, VALUE *argv) {
return dummy;
}
VALUE method_compose(VALUE self, VALUE other_proc) {
VALUE procs = other_proc;
procs = rb_ary_push(procs, self);
VALUE result = rb_proc_new(compose, procs);
FILE *f = fopen("clog.txt", "w");
if (f == NULL) {
printf("no log file found\n");
exit(1);
}
fprintf(f, "procs: %s\n", RSTRING_PTR(rb_ary_to_s(procs)));
return result;
}
// The initialization method for this module
void Init_compose() {
rb_define_method(rb_cProc, "compose", method_compose, -2);
rb_define_method(rb_cProc, "*", method_compose, -2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment