Skip to content

Instantly share code, notes, and snippets.

@lifo
Created August 21, 2009 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifo/172140 to your computer and use it in GitHub Desktop.
Save lifo/172140 to your computer and use it in GitHub Desktop.
[lifo@null rock (master)]$ ruby extconf.rb && make
[lifo@null rock (master)]$ ruby run.rb
require 'mkmf'
extension_name = 'rock'
dir_config(extension_name)
create_makefile(extension_name)
#include "ruby.h"
#include "node.h"
static ID hook_method;
void method_call(rb_event_t event, NODE *node, VALUE self, ID id, VALUE klass) {
char * called_method;
called_method = rb_id2name(id);
VALUE hook_exists = rb_obj_respond_to(self, hook_method, Qtrue);
if(hook_exists == Qtrue && strcmp(called_method, "method_call_hook") != 0) {
rb_funcall(self, hook_method, 0);
}
}
void Init_rock() {
hook_method = rb_intern("method_call_hook");
rb_add_event_hook(&method_call, RUBY_EVENT_CALL);
}
require 'rock'
class Hello
def world
end
def method_call_hook
puts "My bloody hook"
end
end
h = Hello.new
h.world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment