From: "Mauricio Fernández" Date: 2003-07-07T02:03:42+09:00 Subject: Re: Help with UnboundMethod#bind error On Sun, Jul 06, 2003 at 11:51:04PM +0900, Pit Capitain wrote: > On 6 Jul 2003 at 21:23, ts wrote: > > > >>>>> "P" == Pit Capitain writes: > > > > P> yes, I know that you hacked Ruby and that Matz is also working on it, > > P> but for me using the PragProg Ruby installer on Windows nothing of > > P> those I can use right now. > > > > Write an extension : > > > > (... demo of Guy's secret code in action ...) > > This is the real Guy Decoux we all love and respect! > > I know you can do it and maybe I would be able to do so, too, after > spending enough time on it. Thank you for trying to motivate me to > come up with my own solution. But I'm sure this won't happen. So I'll > be waiting for Matz's code. Maybe Nobu will find some time to tell me > why my pure Ruby solution doesn't work. The extension is easy to write (although you'll have to wait for Guy to check if my code is a time bomb :-): override.c: =================== #include #include #include static VALUE class_override(VALUE self, VALUE symbols) { int len; int i; VALUE meth; VALUE name; st_data_t data; NEWOBJ(proxy, struct RClass); OBJSETUP(proxy, rb_cClass, T_ICLASS); len = RARRAY(symbols)->len; proxy->iv_tbl = st_init_numtable(); /* not needed? */ proxy->m_tbl = st_init_numtable(); proxy->super = RCLASS(self)->super; RBASIC(proxy)->klass = RBASIC(self)->klass; for(i = 0; i < len; i++) { meth = rb_ary_entry(symbols, i); name = rb_funcall(meth, rb_intern("to_s"), 0); if(!st_lookup(RCLASS(self)->m_tbl, rb_intern(RSTRING(name)->ptr), &data)) continue; /* should do something else?? */ st_insert(proxy->m_tbl, rb_intern(RSTRING(name)->ptr), data); } RCLASS(self)->super = proxy; OBJ_INFECT(proxy, self); return self; } void Init_override() { rb_define_method(rb_cClass, "override", class_override, -2); } =================== extconf.rb: =================== require 'mkmf' create_makefile("override") =================== And then... batsman@tux-chan:~/src/override$ expand -t2 b.rb #!/usr/bin/ruby require 'override' class C def m puts "original" end end C.new.m # => original class C override :m def m puts "before(1)" super end end C.new.m # => before(1), original class C override :m def m puts "before(2)" super end end C.new.m # => before(2), before(1), original batsman@tux-chan:~/src/override$ ruby b.rb original b.rb:14: warning: method redefined; discarding old m before(1) original b.rb:24: warning: method redefined; discarding old m before(2) before(1) original -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Anyone want the new supermount? :) whats new aboutit klogd: It cleans whiter than white. :) -- Seen on #Linux