From: nobu.nokada@... Date: 2004-01-21T08:20:00+09:00 Subject: Re: enable debugflag in module Hi, At Wed, 21 Jan 2004 06:55:01 +0900, Simon Strandgaard wrote: > class Object > # enable the global debug flag for just a single method > def self.debug(*args) Should be Module's instance method. > #{org}#{arguments} > $stdout.puts("after ##{name}") This discards the result of the original method. And you'd better to check the method already "debugged" to get rid of infinite recursion. class Module # enable the global debug flag for just a single method def debug(*args) debug_methods = private_instance_methods(true) args.each{|symbol| name = symbol.id2name org = "_debug_"+name if debug_methods.include?(org) $stderr.puts "ERROR: already debugging method: #{name}" next end begin meth = instance_method(symbol) rescue => e $stderr.puts "ERROR: symbol2method failure, " + e.inspect next end alias_method org, name module_eval %{ def #{name}(*a, &b) $stdout.puts("before ##{name}") debug, $debug = $debug, true result = #{org}(*a, &b) $stdout.puts("after ##{name}") result ensure $debug = debug end private :#{org} } } end end class Test module A def test p 42 end debug :test end include A debug :test # Error and skipped end Test.new.test -- Nobu Nakada