Archive for the 'Programming' Category

LOLCODE

Tuesday, May 29th, 2007

lolcode:

IZ VAR BIGGER THAN 10? KTHXBYE

Excellent (thanks Carl).

Hi I’m Ruby on Rails

Wednesday, May 16th, 2007

Another funny Ruby on Rails/PHP video:

Datastructures blog

Tuesday, May 15th, 2007

It is fairly old news and hasn’t been updated since March 1st, but I have to post about datastructures.info. I really love the visualisations, for instance from What are Hash Tables and how do they work?

hash tables

Hi I’m Ruby on Rails

Tuesday, May 15th, 2007

A couple of funny videos in the style of the new Apple adverts:

Rails and Java

Rails and PHP

PLZ HLP I HAV QRY LIKE THIS WHCIH SUM1 ELSE WRITE

Thursday, April 26th, 2007

This oracle forum post is way too good not to be a troll. It contains some real gems:

PLZ KINDLY ANZWER NOW I DO NOT KNOW ANSWER. DO NOT SAY TO READ DOCS I DO NOT WNAT THE BOTHER. ANSER PLZ. I AM SUPPOSE TO KNOW SQL BUT I DONT KNOW WHAT IS THE ANS.

and:

Message was edited by:
user571114

PLZ DO NOT WASTE MY TIME

Getting RMagick working on Fedora Core 6

Monday, April 23rd, 2007

I needed to install rmagick over the weekend as I was playing with the file_column plugin. Rubygems has usually been good to me so I didn’t expect any problems when I issued the command:

#gem install rmagick
<snip>
/usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/misc.rb:321:in `get_type_metrics': unable to read font `/usr/share/fonts/default/TrueType/verdana.ttf' (Magick::ImageMagickError)
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/misc.rb:321:in `render'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/misc.rb:696:in `text'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/text.rb:65:in `add_primitives'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/container.rb:72:in `add_primitives'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/container.rb:72:in `each'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/container.rb:72:in `add_primitives'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/rvg.rb:264:in `add_outermost_primitives'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/rvg.rb:264:in `each'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/rvg.rb:264:in `add_outermost_primitives'
from /usr/lib/ruby/gems/1.8/gems/rmagick-1.15.5/./lib/rvg/rvg.rb:236:in `draw'
from InitialCoords.rb:22
post-setup.rb: InitialCoords.rb example returned error code 256

Uh-oh. It seems I was missing some windows specific fonts. Well obviously, I am using Linux. With no obvious package with the missing fonts available in yum I went looking and found this windows font rpm.

After installing and linking the font dir to the place rmagick expects them:

ln -s /usr/share/fonts/msttcorefonts /usr/share/fonts/default/TrueType

That done #gem install rmagick works!

Getting ActiveRecord-like ‘created_at’ behaviour with Perl’s Class::DBI

Thursday, November 2nd, 2006

I really like ActiveRecord and on a recent project I wanted to be able to create fields in my database called ‘created_at’ and have them automatically filled with the current date at time of insert just as ActiveRecord does. Unfortunatly I was forced to use Perl’s Class::DBI* so I came up a simple method of creating the same behaviour.

First, create a parent class that all of your Class::DBI table classes will inherit from:

package DB::DBI;
use base 'Class::DBI';
use POSIX qw(strftime);

#Your connection string will vary:
__PACKAGE__->connection('dbi:mysql:database=db_name;host=127.0.0.1',"username","password");

# Automatically add timestamp to 'created_at' fields.
__PACKAGE__->add_trigger(before_create => sub {
my $self = shift;
$self->can('created_at') and $self->set('created_at', strftime("%Y-%m-%d %H:%M:%S", localtime));
});

Now create a class for each of your tables inheriting from the parent class:

package DB::MyTable;
# Inherit from DB::DBI
use base 'DB::DBI';

__PACKAGE__->table('my_table');
__PACKAGE__->columns(All => qw/id field1 field2 field3 created_at/);

And now when you do a DB::MyTable->create({...}) your created_at field will be automatically populated with the current timestamp. Yay!

* And no, I wasn’t allowed to use DBIx::Class.

[Perl|Ruby|Python|PHP] Golf

Friday, July 28th, 2006

I have recently been playing with carl’s new project, http://codegolf.com/

Basically the idea is to solve given programming challenges in as few characters as possible. The output from entries is compared to a reference output and any that pass the comparison are scored by the number of characters they used. Some challenges require you to parse input aswell.

The implementation is quite neat. Code you upload is run as an unprivileged user inside a chroot inside a jail in FreeBSD, and the whole lot is re-created for every run. Four languages are available, Ruby, PHP, Perl and Python with brainfuck (wikipedia entry) support promised soon.

Anyway, if you have a spare afternoon/evening/week it is pretty good fun.

If that sounded like an advert it wasn’t meant to :)

Set the format of ‘Date’ fields returned by Oracle

Wednesday, July 19th, 2006

For some reason Oracle returns the date from ‘date’ fields in the format ‘dd-short_month_name-yy’ (or something like that) by default. This is crazy, who uses the date in that format? No-one, that’s who. To change the format of dates returned for all queries returned to a database handle do something like this:

$dbh->do("alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'");

(or the equivalent in your language of choice) and the date returned from any date fields will be sensible, much like the way postgres does it. And why does Oracle not allow the use of limit? I guess this comes as a £10,000 upgrade or something.

How to get the SQL that Perl’s DBI is running

Wednesday, July 19th, 2006

It seems to be a question that comes up fairly often, and it is pretty easy:

$dbh->{TraceLevel} = "SQL";