Dec 202008
 

Taking the code from this other blog … It’s pretty elegant Ruby!

I won’t waste your time repeating what the guy wrote in his blog – you’re welcome to go read it. I just felt that I should help spread a little this elegant implementation of the standard permutation algorithm, fixing a small bug within it in the process. If, like me, you have issues understanding how to use this, well – you have to use this function and call a block of code on it. It runs the block of code on each permutation it finds.

def permutations array
  if array.sizeĀ < 2
    yield array
  else
    array.each do |element|
      permutations(array.select() {|n| n != element}) \
      {|val| yield([element].concat val)}
    end
  end
end
Nov 042008
 

Whoo! I haven’t done a post that long in a while! Also makes me think I should get a wordpress plugin for some ‘code’ tags..

I put ‘activerecord’ in there to take advantage of the ’24.hours.ago’ notation, which makes life much easier. The cost is a little less than 2 seconds to load the library, so I think it’s worth it. It runs as a daily job before backup to tape, to clear old backups from the directory tree.

require 'activerecord'

def delete_recursively(in_here)
  Dir.chdir(in_here)
  Dir.glob('*') do |filename|
    if File.directory?(filename)
      delete_recursively(filename)
      else
        if File.mtime(filename) > 24.hours.ago
        File.delete(filename)
      end
    end
  end
Dir.chdir('..')
end

delete_recursively("your/path/here")