Category Archives: ruby

Ruby utility script – csv2json

A script that I wrote and am using more and more.

Arguments are “filename.ext”

require ‘json’

source = ARGV[0]
if source.nil?
puts “Missing filename argument”
puts “USAGE: #{$0} filename.ext”

unless source && FileTest.exists?(source)
puts “#{source} not found!”

puts “Processing ‘#{source}'”


file =,’r’)
ext = File.extname(source).to_s.downcase

outname = source.sub(ext,’.json’)
out =,’w’)

sep = case ext
when ‘.csv’
when ‘.psv’
when ‘.tab’
# assume comma since it is *CSV*2json
puts “Extension: #{ext} using ‘#{sep.source}'”

lines =\n\r?/)

header = lines[0].split(sep)
data = lines.drop(1).map {|item| item.split(sep)}

outdata = []

data.each do |item|
h =
for i in (0..(header.size – 1))
h[header[i]] = item[i]
outdata << h end out.write outdata.to_json out.close puts "File written to " + out.path

Installing Rails 3 on Ubuntu 11.04

One of the more challenging problems with the opensource world (and joys in my opinion) is the rate of change of the software and systems. Unfortunately it means that every year I seem to be having to learn new ways of installing the latest tools. Every 6 months when I install the latest Ubuntu and Rails, something needs hand holding. Here is my attempt to get Rails running on Ubuntu 11.04.

Firstly, credit goes to yasi8h from theKindOfMe blog – see the original post here

Below is a variation of the process outlined in the blog post, however I have changed things to make it work for a fresh Ubuntu 11.04 install.

Run the following in sudo/root

apt-get install curl git-core build-essential bison openssl libreadline5 libreadline5-dev zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libmysqlclient-dev mysql-client mysql-server autoconf

curl -L > rvm-install-head
bash rvm-install-head

Read and follow the instructions given as shell output after  rvm-install-head to edit your .bashrc. You will need to remove the return statement and wrap the whole code block in if’s and append the command to load the rvm into the shell environment. 

OR suggests running:

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile

rvm install 1.9.2
rvm use 1.9.2 –default
rvm 1.9.2 –passenger
rvm 1.9.2
gem install passenger
apt-get install libcurl4-openssl-dev
rvmsudo passenger-install-nginx-module

gem install rails

rm rvm-install-head

Ruby Tutorial #2

There are very few programming fundamentals but an infinite way of using and expressing them.

I will start with two basic types – Strings and Integers.

A string is an ordered sequence of zero or more characters.
An integer is any whole natural number.

Ruby is a weakly typed language meaning that you don’t need to predetermine a variables type at declaration, rather it is dynamically set (and reset) at assignment.

message = “hello world” #string
puts message
puts message.class

message = 42 #integer/fixnum
puts message
puts message.class

The key parts of the above script are:
message: a variable
puts: prints to the console
#: a comment. Anything following the # is ignored.
.class: is a method of Object (message in this case)

Don’t worry if all these terms don’t make complete sense yet.

Some other methods to try out are:

puts message.reverse
puts message.upcase
puts message.length
puts message[0..4]

Strings can be changed and manipulated. The plus sign is concatenation eg.

message = “hello world”
message = message[0..5] + ‘mum’
puts message

Appending a ! on the end will change the contents of the variable in place.

message = “Hello Mum”
puts message
puts message

Ruby treats everything as an object. For example

puts “hi mum”.length

And you can chain methods together

puts “Hello muM”.upcase.swapcase

Beginning Ruby in Netbeans 6.5.1

The most recent versions of Netbeans (6+) have become very usable and are improving rapidly. Probably not as feature rich as Eclipse but faster and certainly not lacking in professional capability. Netbeans is currently my IDE of choice for ruby and rails development.

I intend to write a series of tutorials for my brother to gently introduce him to programming in ruby.

Lets begin.

Download Netbeans here. You have two choices you can get the Ruby package (about 60mb) or the whole suite (240mb).

Tutorial 1.

  1. Launch Netbeans
  2. File -> New Project..
  3. Select “Ruby Application”. Click “Next”
  4. Give your project a name and a location to save it. In this instance I am calling it “RubyTutorial” and saving it in my “workspace” directory. Click “Finish”.
  5. Now you are in the project environment. In the right pane the tab should say “main.rb”. Click on the “Run” button (or press F6)

The Output pane should be visible now with the words “Hello World”

Congratulations you have just run a basic ruby program.

In the code pane you will see the line:

puts “Hello World”

You can change what is between the quotes to anything you like. Press the run button (or F6) to see what happens. Any line beginning with # is a comment and won’t affect the execution of the program.