Sunday, February 25, 2007

Distributing Ruby Apps on Windows

So, you've developed a Ruby script or application for Windows. You'd like to compile the application into a portable executable file (EXE) that you can easily provide to your users without requiring them to install Ruby and the required libraries. Reach for RubyScript2Exe, Erik Veenstra's Ruby compiler. RubyScript2Exe traces and gathers all the necessary files, including the Ruby interpreter, and 'compiles' them into a single EXE file. You can easily embed images and icon files, and DLLs such as SQLite. RubyScript2Exe is easy to configure, very reliable, and well-documented and supported by the author. Executable files are 3-4mb in size for a typical wxRuby (0.6) application.

UPDATE: I have posted a new article with further details on using RubyScript2Exe, here.

To make compiling your Ruby apps even easier, install LopeSoft's free File Menu Tools and add a new file menu command for RubyScript2Exe. Then just right-click on your Ruby script in Windows explorer and select 'Compile with RubyScript2Exe'.

Speaking of compiling Ruby apps on Windows, keep an eye on the Gardens Point Ruby.NET Compiler project at Australia's Queensland University of Technology. I haven't done much with it yet but it looks promising.

Thanks for stopping by!

Digg my article

Saturday, February 17, 2007

Simple Output to Excel-Compatible Files

Need to output data to an Excel-compatible file -- but don't need to do any formatting of the resulting worksheet? You could simply write the data to a tab-delimited text file and give it the ".xls" filename extension...


# where recordset is a database recordset,
# array of arrays, etc:
xls = open('worksheet.xls', 'w')
recordset.each do |row|
xls.puts(row.join("\t"))
end
xls.close

There are, of course, several alternatives involving CSV, XML, or WIN32OLE. Sometimes I will output the data to a tab-delimited text file, then use WIN32OLE to open the file in Excel, apply formatting, and then save in native Excel format...

EXCEL_FORMAT= -4143
xl = WIN32OLE.new('Excel.Application')
wb = xl.Workbooks.Open('worksheet.xls')
# insert formatting code here...
xl.DisplayAlerts = 0
wb.SaveAs('worksheet.xls', EXCEL_FORMAT)
xl.DisplayAlerts = 1

This is often much faster than writing the data directly to a new Excel workbook via WIN32OLE.

Friday, February 16, 2007

From VB to Ruby - Creating Objects via COM

Many developers looking to take up Ruby on MS Windows have experience with Visual Basic, VBA and VBScript. There are millions of examples of VB code out there, and these can help you get up to speed with Ruby, especially in areas such as Microsoft Office automation. Here are a few tips when moving from VB to Ruby:

  • Require the win32ole library at the top of your script.
  • VB requires the Set statement when binding an object to a variable; Ruby does not.
  • In place of VB's CreateObject function, use Ruby's WIN32OLE.new method.
  • In place of VB's GetObject function, use Ruby's WIN32OLE.connect method to connect to an existing instance of an object.
  • Remove all Dim statements; Ruby does not use them.
So...

Dim xl
Dim ol
Set xl = CreateObject("Excel.Application')
Set ol= GetObject(,"Outlook.Application')

...becomes...

require 'win32ole'
xl = WIN32OLE.new("Excel.Application")
ol = WIN32OLE.connect("Outlook.Application")

More VB-to-Ruby tips to follow. Let me know if there's anything in particular you'd like to see.

Wednesday, February 14, 2007

Hello, and welcome to our show...

The idea behind this blog is to discuss the use of the Ruby programming language on the Microsoft Windows platform. Stay tuned...