From: Bill Date: 2004-10-12T14:14:36+09:00 Subject: Re: Ruby programming styles and new program? Eivind Eklund wrote: > > # > # I'm not entirely sure about the inject rewrite here; just > # accumulating on moved may be just as good. > # > def archive_old_to_zip(backup_dir, base_log_name, t) > dir = Dir.open(backup_dir) > moved = dir.inject(0) do |lcount, ogfile| > # Skip-logic can use trailing conditionals > next unless logfile =~ /^CID/ > next unless t > File.stat(logfile).mtime + ARCHIVE_DAYS_SECS > # Active logic use prefix if > if (move_to_archive(logfile)) > next count + 1 > else > next count > end > end > dir.close > return moved > end > Thanks for the suggestion to put the logging/archiving code in a class. That refactoring does allow more appropriate data scoping. One problem: I cannot find documentation for the Dir.inject method. I assume from some of the Ruby docs this is like the Smalltalk inject method, but I don't know why it's not documented in the Dir class? Anyway, what, exactly, is happening in the line (typo removed) > moved = dir.inject(0) do |count, logfile| and how do you know to put the | count, logfile | variables in that order, and not | logfile, count | ?