From: Jean-Hugues ROBERT Date: 2002-04-12T15:40:18+09:00 Subject: Re: How to Make a Method Ineffective Efficiently? Hello, "an efficient debugging log facility" In debug( msg) you don't want msg to be evaluated when debugging is off. You may use a block that will get called only if debugging and will return the string to print: debug{ msg}. Note the {} instead of (). Example: def debug( &proc ) if @@debug_debugging then @@debug_file.write "Debug: @{proc.call()} end end end then use it: debug{ "An inefficient msg: @{xxx()} The idea is that the &proc gets computed *only* when in debugging mode. You may get some extra speed by redefining debug() using eval() whenever the debugging flag changes. i.e. "def debug() end" when debugging is off. Jean-Hugues At 05:36 12/04/2002 +0900, you wrote: >Hi, > >I am having a problem regarding coding efficiency (both for the execution >time and for the typing itself). Basically, I have a method that may or >may not print depending on a flag, such as > > .... > def print_info (*text) > if @debugging > .... > @file.write (text.join) > end > end > >Originally, I have put the flag in the calling class itself, but it made >me typing the flag in too many places: > > .... > print_info ("#{obj1} #{obj2} .... #{obj1000}") if @debugging #format A > >(because in reality the flag or condition may not be that >short). Therefore, just like in the first fragment of codes, I have put >the flag in the print_info method itself, so that I can simply write > > .... > print_info ("#{obj1} #{obj2} .... #{obj1000}") #format B > >and whether it actually prints or not is controlled by the flag in the >print_info, which I can control without having to type in too many places. > >However, now I have a problem with execution efficiency. With the last >format (format B), whether or not I actually print, the methods to_s of >obj1, ..., obj1000 are called. The methods to_s themselves may be very >complicated. In the first format (format A), the methods to_s are not >called at all if @debugging is equal to false. > >I don't want to type "if @debugging" everywhere (they clutter the codes), >but I also want to have efficient execution, which in this case is >equivalent to if @debugging is false, then the print_info method is >ineffective, not even the passed arguments. Is there any way I can >accomplish what I want easily? > >(My current solution is to use format B, and try to minimize the argument >overhead, by minimizing the number of arguments and doing processing >inside the print_info method below the "if @debugging" line as much as >possible.) > >Regards, > >Bill --------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17