enable/disable
output!
debug_puts
outputs to stdout.
The syntax could be like
temporary_enable(variable_name, methods)Below is a simplified copy of a testcase of mine. The
debug
controls a global variable, which controls when to output debug information. I can easily control what is going on by inserting or removing comments.
class TestScanner < Common::TestCase module Works include ExercisesSequence include ExercisesAlternation include ExercisesRepeat end module Todo include ExercisesRepeatNested debug :test_repeat_nested4 undef test_repeat_nested4 # activate_next set wrong node as DONE debug :test_repeat_nested2 #debug :test_repeat_nested3 #undef test_repeat_nested2 # activate_next fix cause trouble #undef test_repeat_nested3 # activate_next fix cause trouble end include Works include Todo end
temporary_enable
method should probably be added to the class Module
, in order to make it usable within modules.
class Module def temporary_enable(variable, *method_names) temp_methods = private_instance_methods(true) method_names.each do |symbol| name = symbol.id2name org = "_debug_"+name if temp_methods.include?(org) $stderr.puts "ERROR: already enabled method: #{name}" next end begin meth = instance_method(symbol) rescue => e $stderr.puts "ERROR: symbol2method failure, " + e.inspect next end arguments = (meth.arity != 0) ? "(*a,&b)" : "(&b)" alias_method org, name module_eval %{ def #{name}#{arguments} old, $#{variable} = $#{variable}, true return #{org}#{arguments} ensure $#{variable} = old end private :#{org} } end end end if $0 == __FILE__ $global = false class A def test puts "A#test global=#{$global}" end #temporary_enable("global", :test) end A.new.test end
The implementation can also be done using the upcoming hook mechanism, which IMHO makes this RCR superfluous.
-- Robert Klemme
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog