On Sat, 9 Sep 2006, Mauricio Fernandez wrote:

> On Sat, Sep 09, 2006 at 12:20:51AM +0900, ara.t.howard / noaa.gov wrote:
>> On Fri, 8 Sep 2006, Ken Bloom wrote:
>>> On Fri, 08 Sep 2006 14:34:39 +0900, ara.t.howard wrote:
>>
>> the show stopper for me is the fact that define_method cannot define methods
>> that take blocks - so your approach loses them during the alias process.
>>
>> i remember that define_method will soon have the ability to define methods
>> that take blocks do you know if that's in 1.8.5 or the 1.9 branch?
>
> It's been in 1.9 for quite a long time now.

i thought so.  still, i'm unclear whether or not it'll make the task any
easier.  anyhow... i've made progress.  here's the latest:

btw - i like the look of call_stack - good work!

the latest, it gets over the redef issue:


harp:~ > ruby a.rb
"a"
"ab"
"abc"



harp:~ > cat a.rb
class Module
   def child this = self
     @child ||= self.class.new
     @child.module_eval{ include this}
     @child
   end

   def has_child
     defined? @child and @child
   end

   def override m, &b
     this = self

     m = Module.new{
       @m = this.instance_method m
       this.module_eval{ remove_method m rescue nil }

       module_eval <<-code
         def #{ m }(*a, &b)
           um = ObjectSpace._id2ref #{ @m.object_id }
           um.bind(self).call *a, &b
         end
       code

       child.module_eval &b if b # put super into right scope!
     }

     include(m.has_child ? m.child : m)
   end
end

class C
   def m() 'a' end
   p new.m #=> 'a'

   override('m'){ def m() super + 'b' end }
   p new.m #=> 'ab'

   override('m'){ def m() super + 'c' end }
   p new.m #=> 'abc'
end



if course, the cool thing is that it even works for blocks:


   harp:~ > cat a.rb
   require 'override'

   class C
     def m(&b) b.call end

     override(:m){ def m(&b) super + 2 end }
   end

   p C.new.m{ 40 }


   harp:~ > ruby a.rb
   42



now - making it undoable, abstracting, etc.  is a lot more work...


-a
-- 
what science finds to be nonexistent, we must accept as nonexistent; but what
science merely does not find is a completely different matter... it is quite
clear that there are many, many mysterious things.
- h.h. the 14th dalai lama