Thursday, July 28, 2011

Trouble with Ruby calling Powershell

Today I ran into a problem where a Powershell script fails when called inside a Ruby script -- if you are asking why, it's because I have to.
Anyways, the relevant data specs are:
Dell T5500 Dual Quad Xeon
Windows 7 x64 Ultimate
Ruby 1.9.2-p180
Postgresql x64 driver

When the script is ran by itself it works just fine. But inside a very simple Ruby call:

#!/usr/bin/env ruby

#...

if system("powershell -File #{powershell_script}")
  #...
end


My powershell script started failing. After some debugging I found that the reason for the failure was the ODBC connection call inside the PS was failing. But why?
The clue in this problem was the x64 driver used for connecting to Postgresql. Since Ruby 1.9.2-p180 is a 32-bit application it calls the 32-bit version of Powershell which craps out when made to use the 64-bit driver.

So what was the solution? I installed the x64 version of Jruby for Windows (along with the x64 version of the JRE). And now my call to powershell works! Except it somehow hangs...

Replacing system() with IO.popen() solved the problem.

#!/usr/bin/env ruby

#...

out = IO.popen("powershell -File #{powershell_script}")
out.readlines # wait for the program to finish
  #...
end


My main Ruby interpreter is MRI/YARV. But with this recent issue, along with several good feedback I've read on JRuby (such as real threads) maybe I'll use it as the default interpreter.

From what I understand you can call the standard Java libraries inside JRuby, which might be very useful in some use cases.

There are a lot of other languages targeting the Java VM such as Clojure, Scala, Groovy, etc...maybe there's something I'm missing...or it could really be true that the Java VM is one of the best VMs out there.