Playing with Ruby

I don't usually get very technical on this blog. If you like technical, this is for you. If not, you may want to skip this one. I had a friend who wanted to create a simple and somewhat private, but not majorly secure file sharing solution. We decided to solve this by creating a subdomain for them, securing it so that it can't be indexed or looked at by search engine robots and then named all the files according to their MD5 Sum. This is a solution that looks random, but is actually re-creatable if we loose track of what link goes to what file name.

We have a bunch of files to handle, so I wrote this little ruby script to automate the renaming of the files and to give me the links connected with the old filenames. I used some sources around the web to make it so I'm giving it back to the web. :-) I'd be interested in your feedback and ways to improve it. One thing I had trouble with was the setting of the domain variable. I tried to set it as a constant outside the loop, but it seemed to only work a tenth of the time or so. This forced me to set it within the loop, wasting some processes I'd imagine, but accomplishing the task.

#!/usr/bin/ruby

# This script takes the contents of a directory and renames them
# to their MD5 checksum (or digest) excluding anything that starts
# with a "."

require 'digest/md5'

Dir.new(".").each do |file|
unless file =~ /^\./
f = File.new(file)

# include a / on the end
domain = "http://docs.domain.com/"
oldname = f.path
newname = Digest::MD5.hexdigest(file) << File.extname(file)

puts domain << newname << " " << oldname << "\n"
File.rename(oldname,newname)
end
end