Yesterday I’ve forgotten to add one important point to my post about “Everyday ruby”: after developing the script under Windows with ruby 1.8.2 I recognized I could have put it in crontab on a linux server, where several metrics are already gained or calculated every night. When I tried I had a bad surprise: our old linux server was running debian woody, an old stable release dated 2002, on which only ruby 1.6.7 is available. Hence, some of the libraries that I used were not available. For example, I have used new FileUtils module. After 5 seconds of panic I’ve thought that the script could easily recognize which ruby version is running, deciding to redefine a few methods for backward compatibility. It took me 10 minutes to understand what I was missing and write the following:
def is_old_ruby? VERSION < "1.8.0" end require is_old_ruby? ? 'ftools' : 'fileutils' if is_old_ruby? def pwd Dir.pwd end def mkdir_p(path) Dir.mkdir(path) if !File.exist? path end def cd(path) Dir.chdir(path) end else include FileUtils::Verbose end
Ugly, but it saved my day.
It took me some more minutes to understand that I needed to invoke shell commands or external programs differently between Windows and linux. Therefore, with a little help from the following method, I’ve simply modified a few invocations accordingly:
def is_win? is_old_ruby? ? PLATFORM =~ "mswin" : PLATFORM.match("mswin") end
I know I could have done it the same way with other languages, but believe me when I tell you that backward compatibility with statically typed language is a lot more harder, even in the simplest case like this one.
This is another good reason why choosing ruby for your everyday tasks at the border of your programming environment may be a good idea. Even when java pays the bills.