Lennon Day-Reynolds <rcoder / gmail.com> wrote in message news:<5d4c6124040617110925ad6885 / mail.gmail.com>...
> That's not the original source for the hack(s) I was talking about,
> but it seems to cover pretty much all of the cases I mentioned.

Maybe you meant the become smalltalk stuff people were chatting about?


The current version of my code is at
http://www.cs.drexel.edu/~ummaycoc/delayed.rb

Anyway, I'm still having some problems with this code, and I can't
seem to fix it up to work.  There are two issues:

  1) a binding to a method isn't being kept for some reason.  I talk
about @singleton_added.  To see that it's not working, please refer to
http://www.cs.drexel.edu/~ummaycoc/delayed.rb.debug - which has some
debug print statements.  After redefining singleton_method_added - the
call to super should invoke the magic, I think.  Also, unless I call
super, a singleton-method singleton_method_added doesn't call
singleton_method_added - which I thought
was a bit counter intuitive, is it supposed to be like that?

  2) how to bind a method object to an unrelated class (ie not a
subclass).  I'm thinking of hacking the source so that one can go from
method foo -> module containing method foo.  then you could add any
object's method to another object.  I just ask whether if anyone knows
off the bat whether there is a technical issue in the way.
     

Below shows that point 1 should be plausible...
Thanks for any and all help!

------------------------------------------
matt@linux1 matt $ ruby -v 
ruby 1.8.1 (2004-04-24) [i686-linux-gnu]
matt@linux1 matt $ cat test.rb 

class Test
  def initialize
    @method_before = nil
    @method_after  = nil
  end

  def meow
    STDOUT.puts "In before."
    return unless @method_before.nil?

    @method_before = self.method(:meow)
    def self.meow
      STDOUT.puts "In after."
    end
    @method_after = self.method(:meow)
  end

  def before
    @method_before.call
  end

  def after
    @method_after.call
  end
end


x = Test.new

before = x.method(:meow)
x.meow
after  = x.method(:meow)


STDOUT.puts "CALLING!"
before.call
x.before

STDOUT.puts
after.call
x.after


STDOUT.puts
STDOUT.puts "UNDEF"
class << x
  self
end.class_eval {
  undef_method :meow
}

STDOUT.puts "CALLING!"
before.call
x.before

STDOUT.puts
after.call
x.after

matt@linux1 matt $ ruby test.rb 
In before.
CALLING!
In before.
In before.

In after.
In after.

UNDEF
CALLING!
In before.
In before.

In after.
In after.

------------------------------------------