Archive for the ‘Development’ Category

Ruby script for replacing tab delimeters with commas

on Tuesday 11th March, 2008 Gabe speculated thusly…

A trivial Ruby script for replacing any number of tabs in input.csv with a single comma and then writing it as output.csv.

The first version works from the command line and makes use of concatenating a file and piping it to a ruby command which then diverts the output in to a file, a one liner:
cat input.csv | ruby -pe 'gsub( /\t+/, "," )' > output.csv

This can be further enhanced by using the in-place-edit switch of the Ruby interpreter, editing the input.csv file and leaving a pristine copy with .bak extension.
ruby -i.bak -pe 'gsub( /\t+/, "," )' input.csv


The final version is what you might put in to a Ruby file. Although the input and output files are hard-coded, it would be trivial to allow them to be specified on the command line.

#!/usr/bin/env ruby

# Initialise a new file object that is writable
file = File.new( "output.csv", "w" )

# Open the existing tab delimited file and read each line
File.open( "input.csv", "r" ).each do |line|
  # Perform a global substitution of tabs with commas and
  # put that in the new file
  file.puts line.gsub( /\t+/, "," )
end

# Tidy up by closing the newly written output file.
file.close

Posted in Development, Programming, Ruby

No Comments »

Linking images in Code Igniter

on Sunday 17th February, 2008 Gabe speculated thusly…

I’m a big fan of the PHP framework, CodeIgniter. However, until recently it has lacked an elegant way of nesting an image in an anchor tag. I had to resort to manually typing out HTML like this:
<a href="<?=site_url?>/controller/action"><img src="<?=base_url()?>/images/button.gif"></a>
CodeIgniter 1.6 has been released, quickly followed by 1.6.1. This includes an image helper. One is still unable to elegantly link images using CI’s helpers. If you want to be able to do something like this:
<?= anchor( 'controller/action', img( 'images/button.gif' )?>

You could just comment out line 117 in /system/helpers/url_helper.php but that means you are altering CI core, which is not a good idea. For a start it will be overwritten when you upgrade and it is an inelegant hack.

A better bet is to create your own helper file and use it to override CodeIgniter’s one. This is dead-simple, just create a file called MY_url_helper.php in the following location /system/application/helpers/. Note that the MY_ prefix is set in the config.php file under $config['subclass_prefix'] = 'MY_'; so if you have changed that setting you will need to adjust your file-name accordingly.

Then just copy and paste the following in to it: (more…)

Posted in CodeIgniter, Development, Frameworks, PHP, Programming

9 Comments »

Rails Production Environment Using Mongrel Cluster, Apache 2.2 mod_proxy, and Capistrano on Ubuntu Gutsy

on Wednesday 13th February, 2008 Gabe speculated thusly…

Recently, I had to setup a production environment for Rails applications at work. I encountered many difficulties, including problems with permissions on client/server computers, and not least because the documentation I had was out of date (Agile web dev and The Rails Way). Online tutorials were helpful but incomplete. I have compiled all that I discovered here. (more…)

Posted in Development, HowTo, Linux, Operating System, Programming, Ruby, Server

8 Comments »

Andrew Norman: The Book Collection

on Thursday 5th July, 2007 Gabe speculated thusly…

Andrew commissioned me to give his books an on-line presence. He wanted only to list his books and nothing else such as shopping or contact details.This post summarises, and then specifies the project. Designs are presented, and a project plan is demonstrated. Finally, the project is completed and I can make the source code available on request. (more…)

Posted in Development, MySQL, PHP, Programming

No Comments »