Archive for the ‘ruby’ Category



7
Aug

Using Ruby to parse an Excel document

If you work in the finance domain you are often confronted with data in spreadsheets. I’ve used a number of ways to read spreadsheets before like Scriptom (Groovy) or POM (Java). Ruby comes with a windows specific OLE bridge which allows you to use COM to communicate with a number of different MS applications. The pickaxe has more details.

Here is a 4 line working example of opening an Excel document, choosing a worksheet then printing the values of a range:

require 'win32ole'
excel = WIN32OLE::new('excel.Application')
sheet = excel.Workbooks.Open('c:\excel.xls').Worksheets('worksheet')
sheet.Range('A1:A3').columns.each { |col| col.cells.each { |cell| puts cell['Value'] } }
3
Jul

Using Ruby as an AWK replacement

Someone at work asked if you could use Ruby like AWK.  I did a little digging and found that you can.

cat file | ruby -n -a -e 'puts "#{$F[0] $F[1]}"'

‘-n’ makes the Ruby iterate over all lines successively assigning them to $_
‘-a’ makes Ruby split $_ into its parts assigning the result to $F which is an array of strings
‘-e’ means that what follows is code to be executed.
‘-F’ specifies the column separator

I performed a speed comparison on some different size files and operations. For files under 500kb lines Ruby has comparable performance to AWK. For anything larger then Ruby (1.8.6) is at best twice as slow. Though I wouldn’t expect a general purpose language to outperform a specialist tool.