From: laotse@... Date: 2001-06-29T04:56:01+09:00 Subject: [ruby-talk:17028] Wrapping Methods Hello all. I'm still playing around with dynamically mapping methods and ran into a problem I'm trying to work out. I have two versions of a script. One has everything contained in a class file, and works. One has part of the method definitions in a mixin that is included, and it does not work. Here's the code: -- [ CODE THAT WORKS ] -------------------------------------------- class Redef def set_watchpoint(method) original_method = method + "_orig" unless defined? self.original_method self.type.class_eval { alias_method original_method, method } end eval <<-ENDEVAL def #{method}(*args) result = #{original_method}(*args) print "success\n" result end ENDEVAL end def hello print "hello\n" end end test = Redef.new test.hello test.set_watchpoint("hello") test.hello ------------------ this outputs: hello hello success --[ CODE THAT DOES NOT WORK ]------------------------------------------- module Redef def set_watchpoint(method) original_method = method + "_orig" unless defined? self.original_method self.type.class_eval { alias_method original_method, method } end eval <<-ENDEVAL def #{method}(*args) result = #{original_method}(*args) print "success\n" result end ENDEVAL end end class Hello include Redef def hello print "hello\n" end end test = Hello.new test.hello test.set_watchpoint("hello") test.hello ------------------- this outputs: hello hello ----------------------------------------------------------------------------- Sorry for all the questions :) Still struggling to come up to speed on Ruby. If along with a suggested fix, if anyone could suggest what portions of the book (or online docs) I can read to understand why my first code works and my second doesn't, that would be great. ----------------------------------------------------------------------------- Brian Knox Just Another Perl Hacker perl -le '$_="6110>374086;2064208213:90<307;55";tr[0->][ LEOR!AUBGNSTY];print'