Articles about programming

Email address validation is hard

Email address validation is a pet peeve of mine. So much that I’ve built my own library that I can throw into our projects and be done with it.

So you can imagine my disappoint when an error rolled into our exception tracking system the other day (Actual email address changed to protect the innocent):

Mail::Field::ParseError: Mail::AddressList can not parse |ü|
Reason was: Only able to parse up to ü

At first glance it seemed like we’ve allowed an email address to enter our system, that was so invalidly formatted that the excellent Mail gem couldn’t parse it, causing the application to crash instead of sending a friendly “Welcome” email to the new user. And the culprit was a single ü character.

The anatomy of a good commit

Commits are the DNA of your system. They describe the evolution of your system going back to when it was the most basic, automatically generated “Hello World” example. They contain the sum of all knowledge and history of your code.

In many cases they are the least changable part of your system, so you only really get one shot at getting them right. You’d better make it count.

I like my code alphabetically

I have a confession to make. It might be some variant of OCD, or just stupid attention to pointless details, but I like to sort my code alphabetically.

Whenever there is a list of things where the order doesn’t matter, I put them alphabetically. CSS rules, methods, constants, whatever.

Ember on Rails: Validations

In the previous episodes we’ve built a working Ember.js application to manage our growing collection of Books. It lives inside a Rails application which it uses for its backend.

We’re able to list all the books in our vast library, and also add new books to our collection. However, if we try to add a book without giving it a title, the backend gladly accepts it and saves it, which is probably not the optimal behavior.

It is time to add validations to our application.

Ember on Rails

I’ve been trying to get into Ember.js a few times by now, but I’ve always been dissuaded for various reasons.

This time around I’d like you all to learn with me - hopefully the act of me writing down what I do will make it stick better for me.

The plan here is to end up with an Ember application backed by a Rails JSON API - we’ll see if the future agrees with that goal.

I've taken up Exercising

I have been having fun participating in - a collaborative code improvement site started by Katrina Owen.

The rules are simple: You get an exercise with a README and a test suite. You make the test suite pass, submit your code and the other participants can nitpick the submitted code. It’s actually quite fun - even for us experienced developers.

Here are a few of my takeaways from having done a couple of exercises there.

One RSpec stub, many return values

Using RSpec Mocks it is trivially easy to stub a single method:

colors.stub(:favorite).with("Person A").and_return("Mauve")

It is also easy to have different stubs for different arguments:

colors.stub(:favorite).with("Person A").and_return("Mauve")
colors.stub(:favorite).with("Person B").and_return("Olive")

However, the other day I needed to stub out the same method many times for a fair amount of different arguments and repeating the foo.stub(...).with(...).and_return() part gets tedious real fast.

Thankfully, RSpec stubs have a way around this.

How we took our tests from 30 to 3 minutes

One of my client projects have grown a fairly large test suite over the last years and it has become way unwieldy. Our original CI server took more than 30 painstakingly slow minutes to complete a test run.

While we are currently writing most our new tests in the fast test style, rewriting 20.000 lines of slow tests isn’t something that’ll be done in the near future.

So we needed to do something differently that would work here and now.


  • An irresponsible database and more hardware

Destroying anchovies

When ordering pizza from an unknown pizza joint, I used to order a number 7 without anchovies. I didn’t know what pizza number 7 was, but I knew I didn’t want anchovies.

The ensuing dialoge usually went like:

Them: “But… number 7 doesn’t have anchovies?”
Me: “Perfect!”
Them: “So… you want a number 7?”
Me: “Yes. Without anchovies.”
Them: “…“

I didn’t care about the pre-condition of my pizza; I only cared about the post-condition, where it should be without anchovies.

The pizza baker did care about the pre-condition; if the pizza did have anchovies, he’d have to remove them. In his mind, removing anchovies from a pizza without anchovies wasn’t a request he could handle. In my mind, if the pizza didn’t have anchovies, the request had already been fulfilled.

Ruby SSL certificate verification errors

On a client project, we had recently installed capistrano-campfire to get notifications in our Campfire chatroom whenever a deployment takes place.

Unfortunately I kept getting

SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)

when I tried deploying. There’s nothing quite like starting the year with SSL issues…

According to this article the problem

… comes from the fact that the new Ruby 1.9 installation doesn’t find the certification authority certificates (CA Certs) used to verify the authenticity of secured web servers.

I my case, I was using Ruby 1.8 (well, REE) on OS X Snow Leopard, but the problem - and solution - was the same nevertheless.

Digging into Objective-C (again)

While the concept of writing native Mac apps in Ruby definitely appeals to me, I must say the experience isn’t quite as easy as I was looking for.

After writing my last post I realized that what I had thought to be the path of least resistance, wasn’t. And while I knew I’d run into resistance with XCode and Objective-C as well, at least I’d be doing things the “proper” way and learning new stuff.

So I fired up XCode once again and set out to write a Mac application. The following is a bunch of notes and stray thoughts I scribbled down during those first hours - my first (well, fourth) impressions of developing iApps coming from Ruby.

The state of Ruby in Denmark

Yesterday marked the 5 year anniversary of Copenhagen Ruby Brigade. Five years ago, I was part of founding that group. Five years, imagine that.

Back then, you could have heard me claim I knew all the Ruby developers in Denmark. While probably an exaggeration, it wasn’t far from the truth. Thankfully that is no longer the case. Yesterday, as I was sitting in the conference room at Podio looking at the other people present, I realized I didn’t know half of them. This got me thinking about how far we’ve come in the danish Ruby community over the last years.

Announcing r-conomic

If you have ever tried to integrate with e-conomic you’ll likely recognize that their API comes out pretty high on the How to make a crappy API checklist.

Luckily, if you’re using Ruby, you can now use the r-conomic gem to handle SOAP and the other tedious bits for you.

I’ll let the README do the talking while I head on over to my Working With Rails profile and finally check off the “Has published a Ruby gem” checkbox.

How to make a crappy API

So you want to add a web service API to your web application. Because, you know, that’s what cool startups do (when they are not busy pivoting).

Unfortunately creating a useful API is hard work and you’d have to maintain it and support it and that’s even more hard work and look at the time and the wife is waiting…

So what does a lazysmart developer do? You make your API so terrible that no one wants to consume it. Just follow these simple tips.

Ruby: How to check if a String is numeric

Finally, a technical article… This one caused by a seemingly simple question on Stack Overflow, asking how to Test if string is a number.

It turns out that it’s not entirely that simple to answer, and there are lot of apparently correct ideas, that all turn out to fail closer scrutiny.

For those who just want the answer: As far as I can tell, the best method is the one proposed on RosettaCode and in the Stack Overflow question above:

class String
  def numeric?
    Float(self) != nil rescue false

If you want some more details, continue reading.

Localizable Rails 3 validators

Rails 3 introduced a new style of validators that allow us to group validations for each attribute into their own line. It also became easy to create our own, custom validators.

This a small tip, perhaps even a best practice, when doing so.

Most the examples I’ve seen around the web ends up adding errors to the errors array using something like

record.errors[attribute] << (options[:message] || "is not valid")

Unfortunately doing it this way means your validator doesn’t behave like the standard validators and the error message cannot be localized.

Instead, please use

record.errors.add(attribute, (options[:message] || :invalid))

Using a Symbol rather than a String lets you localize the validation message via your standard Rails I18n mechanism. It also lets you pass a proc to :message, allowing for things like to be used within an error message.

Thank you in advance from those of us who create non-English apps.

Tracking exceptions with Redmine

Staying on top of errors that happen in your production Rails applications is a must. Unfortunately trawling through log files get old really fast, and getting enough information about where the error happened can be hard.

That’s what services like Honeybadger or Airbrake solves. Whenever an error occurs in your application the details are sent to their service, where you are able to see when, where and how many times a given error has occurred.

However, if you’re uneasy about sending this level of details to a third party service – or if you’re simply a cheapskate like me, there is another way using Redmine.

The small change that wasn't

The other day a client of mine asked me to make a small improvement to their application. This was no big deal and only required a single string column with some data being added to a table. An hour development time tops, so I said sure.

But then the all-seeing eye of our system administrator caught my little change. Turns out those normalization forms I once learned about actually matter – who’d have thunk?

Humor driven development

Happy developers are productive developers. Having fun makes people happy. So sayeth the Chief Happiness Officer.

This should come as no surprise to noone in the Rails community – after all, Rails is “optimized for developer happiness” and DHH has touted this on numerous occasions.

And the Ruby community in general seems to be a life-loving bunch. _why didn’t rise to fame solely because he was a good programmer. His code was fun and quirky, his documentation and presentations even more so.

Rails 3: Responding with Excel

The following is a short code snippet from my Rails 3 presentation at Community Day last week. I’m putting it here, because it’s such a nifty little piece of code.

It answers an feature request I seem to get fairly often; “please make it possible to download an Excel file of data”. Traditionally I’ve relied on good ole CSV for that, but it’s a bit tricky to get just right with Excel. Besides, the customer explicitly said Excel file, so let’s give her that.

Suppress warnings from Ruby

We Ruby programmers routinely do things that other programmers frown upon, and – in many cases – won’t even be allowed to do by their interpreter/compiler.

Case in point for this article, is redefining constants. In Ruby a constant isn’t constant. Creating a constant is merely a way for the programmer to indicate a value that really isn’t supposed to be changed – but if you do anyways, you’re on your own.

5 ways to run shell commands from Ruby

Every so often I have the need to execute a command line application from a Ruby script. And every single time I fail to remember what the different command-executing methods Ruby provides us with do.

This post is primarily a brain dump to aid my failing memory, and it was triggered by an issue with my Redmine Github Hook plugin where STDERR messages were not being logged.

The goal of this is to figure out how to run a shell command and capture all its output - both STDOUT and STDERR - so that the output can be used in the calling script.

Find missing translations in your Rails application

I am currently wrapping up a client-project where I am preparing a Rails application for internationalization. The application is currently in English and I am translating it to Danish as a proof of concept.

I am using the I18n::Simple backend and the resulting Danish locale file is now at 2100 lines. That’s a bit much for me to keep in my head at once, so I created a small Rake task that takes all locales in your Rails application and reports back all the keys that are missing from one or more locales. Works wonders for finding those typos or oversights that would otherwise result in ‘missing translation data’ messages.

Using Google Page Speed to optimize websites

Back when Yahoo! released their YSlow add-on for Firebug, I took it for a spin and optimized using it.

Google recently released their variant of YSlow called Page Speed. Like YSlow, it’s an add-on for Firebug and it provides a bunch of recommendations for optimizing the clientside performance of your websites. This time, I’ll let it loose on the homepage of Lokalebasen.

Java kicks Ruby in the what now?

When I first read the Java Kicks Ruby on Rails in the Butt article by Javier Paniza, I brushed it aside as something from the time when Rails was just getting traction and people using related, established technologies started feeling threatened.

I then noticed the date – turns out the article is only a few days old! Color me confused, could that article really have been written in 2009? I thought we had left the FUD-spreading behind us.

In an effort to educate Javier – and hopefully others – I decided to rewrite the Rails part of his article the way he would have written it had he had more knowledge and experience with Rails.

Side job versus dayjob

I have been moonlighting as a freelance webdeveloper for the last couple of years. The first year, I was using my spare evenings and weekends until I began having trouble finding enough time to dedicate to new projects.

My reaction was to cut down on my dayjob, leaving me with more time to focus on my own clients (and less time to focus on BiQ, forcing us to hire another developer).

Rails Camp DK 2008

Take 12 attendees (one from Italy, way to go, Fransesco!), 11 Apple laptops (and one Windows one, I believe), 1 wifi hotspot, mix it all together in a cottage in the middle of nowhere, sprinkle it with a tad of Ruby, Git, and *jour and let is stew for 2 days and 2 nights.

What you get is a Rails Camp, and the above recipe was cooked with great success last weekend. Thomas Watson has a bunch of pictures from the event.

ImmediateFeedbackFormatter - Better formatted RSpec output

Update: If you want something like this for RSpec 2, use Fuubar

On one of my projects the specs are now taking a full 10 minutes to run on my machine. Needless to say, it’s mightily annoying seeing a spec failure in the output knowing you’ll have to wait for 10 minutes before you get the details of what’s failing and you can get to fixing it.

Luckily my co-worker, Laust Rud Jensen, had that same itch and decided to scratch it. Unfortunately he doesn’t have a website to post about these things, so I am posting it for him:

On Rails 2.0, SQL Server adapter, gems, vendor and load paths

Over the last few days I have been moving the BiQ application to Rails 2.0 PR. I’ve so far run into a fair deal of issues; one annoying one that I figured I’d share.

The gist of the matter is that creating some of my models failed in some cases. It appeared to be primarily when the save triggered an insert into another table as well.

Digging around a bit, I traced it down to the fact that the newly created model was given an id that didn’t match the id in the database. The reason for that turned out to be quite the sneaky little bastard…

Looking for patch +1's

During his keynote today, and now on the Rails blog, David explained the new policy for getting patches into Rails.

Basically the new policy requires every patch to have documentation, tests and 3 supporters. It’s a welcome change, especially if it means the core team have to do less legwork and apply higher quality patches.

Therefore, I am now looking for a few supporters for some of my patches. If there’s anything of the below you’d like to see included in Rails, please comment on the patch, saying +1.

I have a few other patches sitting idle, but they might need a bit more work (read: tests) before getting supporters.

Using YSlow to optimize websites

Yahoo! has released a great Firefox addon – or rather, an addon for a Firefox addon – called YSlow. YSlow allows you to analyze and suggest improvements to various performance metrics on the website you’re currently visiting.

For kicks I decided to let it loose on Unfortunately it gave me a disheartening grade of F:

Performance grade: F (46)

Ah well, sounds like a perfect opportunity to improve, and the rest of this entry contains the areas where the frontpage did not receive an A-grade, and a description of the modifications I had to perform on our setup to satisfy the addon.

Prototype and TEXTware QUICKfind vs Internet Explorer

You know we’re sitting on four million pounds of fuel, one nuclear weapon and a thing that has 270,000 moving parts built by the lowest bidder. Makes you feel good, doesn’t it?

After launching the rewritten and redesigned BiQ I got word of seemingly random IE installations crashing within seconds of opening BiQ. I could not find a way to reproduce this and it only occured on a tiny minority of client installations, so I shrugged it off as yet another reason to upgrade to Firefox.

Launch: BiQ on Rails

After 6 months of evangelizing and advocating, followed by 6 more months of rewriting, and then 3 more months of building new features and enhancing existing ones, we have finally launched the new version of BiQ.

Going from ASP/VBScript on Windows 2000 and IIS to Ruby and Rails on Debian served with love by Apache proxy balancing to a Mongrel backend, using memcached for relieving the database of those common queries, with continuous integration provided by Cruise Control and easy deployment through Capistrano sure as heck feels good.

Add to that the fact that the new look is quite delicious and powered by standards compliant XHTML sprinkled with a tiny amount of Ajax and a hint of microformats and Atom feeds, and has a blog and some new killer features and you’ll probably understand why I am happy.

Now it is time to make the site even better. ;)

Rails 2.0 deprecations

The Rails core team is cleaning up. That’s obviously a good thing, however it does leave us facing a kabillion deprecation warnings when running tests and whatnot.

Unfortunately the official deprecations page appears to have been thrown together quickly after someone noticed that Rails 1.2 had been pushed out without anyone finishing the page that all deprecation warnings were referring to.

So what does a smart Rails developer who wants to be ready for Rails 2.0 when that time comes around do? She reads on…

Online payments are still a mess

This is a followup to my previous rant about the state of accepting online subscriptions.

A few things happened after my post. Most importantly the client met with a focus group of potential users, and – among other things – talked pricing with them. Every single one stated they’d much rather prefer paying a one-off yearly fee instead of automated monthly subscriptions.

One reason to upgrade Ruby to 1.8.5

Last week I took a quick executive decision to deploy BiQ on Debian Etch instead of Sarge as initially planned. Mainly so we could take advantage of Ruby 1.8.5 (let me hear you scream “Mongrel”) and Subversion 1.4.something.

Today, yet another reason for going with Etch just bit me in the behind while using our development machine:

Simplifying my Rails views

Rails views are powerful creatures. You have the full power of Ruby right at your fingertips, which makes it easy to write too much Ruby code in your view making them become cluttered and hard to read.

The following is the tale of how I cleaned up a small part of a single view in BiQ after having made a dirty mess of it.

Accepting online subscriptions is a mess

Payment gateways, merchant accounts, transaction fees, statements, grraaaah, it’s driving me bonkers.

Here’s the deal: I have a client wanting to sell subscriptions for his web application. Nothing fancy, everyone’s doing it. It should be easy finding a payment gateway supporting that, right? I imagine it is once you manage to wrap your brain around the confusion that is payment gateways and merchant accounts.

Google Code Search launches

Google Code Search is a new search on Google Labs specifically for searching through, well, source code.

And what do the geeks do with it?

Awesome. I am sure there must be some legitimate uses for the search, though, and it’ll be interesting to see what creative geeks will create with it.

Slowing down tests

There I go again, trying to be too clever for my own good. In an attempt to do less I tossed the fixtures loading into test_helper. I figured it’d be a lot easier to just add to that single fixtures line whenever I added a new model, instead of having to potentially add to a bunch of test files.

And it was easier, but it came at a price.

An actual helpful error message

Wow, an exception error message that actually suggests a fix for the error, and the fix is correct. How great is that?

ActionController::RedirectBackError: No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify @request.env[“HTTP_REFERER”].

That sure beats “Unspecified error 0×0057867”.

SQL Server 2000 seems confused

So I am getting this error in my Windows Application Event Log:

The log file for database ‘tempdb’ is full. Back up the transaction log for the database to free up some log space.

Fair enough, it happens. But when I go to backup the tempdb database using Enterprise Manager, I get this message:

Backup and restore operations are not allowed on database tempdb.

Someone tell that to whoever wrote the event log message. :)

The solution?

The proper solution is likely to manually grow the tempdb files to a more appropriate level than the current. Ah well, I’m no DBA, and this seem to have solved the issue for now:


For some reason that command completes without errors, even though "Backup and restore operations are not allowed on database tempdb. There definitely seems to be some confusion as to what’s allowed and what isn’t.

MSDN hates Firefox

Microsoft, a company that proclaims to be devoted to web standards, has updated parts of their MSDN Library VBScript Language Reference. They’ve hidden the most valuable details of each funtion behind some pointless click-to-expand links. Apart from being a usability blunder, Microsoft once again fails to prove that they care for or have even heard of standards or other browsers than Internet Explorer.

Here’s my Firebug log from simply opening the reference page for the DateAdd function.

The MSDN site is now worthless in Firefox since the expand details links does not work. I for one cannot be told what the arguments mean, since this is what my Firebug log tells me when I click the link:

Microsoft, I can’t wait until I no longer have to developer using your shoddy products. It looks like you don’t want me around, anyways. Whatever happened to Developers, developers, developers or was that just Ballmer blowing smoke as usual?

Phew, Rails Day is over

After 24 hours of insane, non-stop coding, designing, talking, and general tom-foolery, Rails Day 2006 is over. Nearly 200 teams competed, committing over 5000 total changesets to the official SVN server.

My team

Even though I got a team fairly late, and our initial design/concept wasn’t fully fleshed out, I reckon we got fairly far. The application was inspired by Dan Russells post on Creating Passionate Users about visualizing your life as dots.

The application got almost feature complete, although it got far from as usable as I would have liked it to. We’ll have it deployed to a webserver near you soon. Our team stats are here, for those who care.

Is Rails Day 2007 coming soon?

Rails Day was a really cool (and tough) experience, and I had a lot of fun. Thanks should go to my teammates and our “manager” (yes, Johannes, that’s you ;) ), and to the Rails Day crew. The spectate function, SVN bots, and generally everything has been running pretty smootly, cheers! Also huge thanks to my lovely girlfriend who took good care of me all day, and didn’t even once call me crazy or roll her eyes – at least not while I noticed.

Next year, I am hoping the danish Rails community will be crawling with people so we can field a team all sitting together. That’d be extremely cool in my book. So, see you next year?

Railsday is acoming

This coming Saturday will bring about 24 hours of Rails programming mayhem. Participating would be extremely cool and fun to boot, but I have no team or no really good ideas for a killer application.

So uhm, anyone need a Rails developer with a flair for UI design?

Update: That didn’t take long, looks like I’ll be competing in Rails day for the first time. Cool, looking forward to this.

Benchmarking Ruby, DBI, and ODBC

Also known as “The blog post where Jakob publically ridicules himself pretending to know squat about benchmarking”

As part of our investigation into changing platforms, we’ll be looking for ways to make Rails or Ruby break down and cry. We’ll be kicking groins, pinching nipples and generally be meanies. Todays target is Rubys DBI/DBD connection to a remote Microsoft SQL Server.

Watch out for those return values

For the first time a Rubyism has bit me using Rails. I needed to make sure a field was set to false and not null. So I added af before_save callback to my ActiveRecord model:

before_save :set_defaults
def set_defaults
  self.signup_completed = false if self.signup_completed.nil?

This worked great and the testcase passed. Alas, a ton of other testcases failed. Somehow adding the above prevented objects from being saved for no apparent reason. They were valid, .errors were empty, but .save still returned false.

“Jakob”, I asked to myself, “what can prevent an ActiveRecord object from saving?” “Oh, many things”, came the swift reply, “for example callbacks returning false, but that’s certainly not the case here.”

However, that is exactly the case here: Ruby methods return the last evaluated result. Since self.signup_completed = false returns false, my callback was returning false as well, which cancels the save. Doh!

The solution

def set_defaults
  self.signup_completed = false if self.signup_completed.nil?
  return true

Ugly, but working.


At the recent Canada on Rails conference Robby Russel introduced his upcoming plugin, Acts as legacy. Facing a 60+ table legacy system, I will definitely be watching that space. Acts_as_legacy would allow me to not worry about our existing scripts and jobs, while still rolling BiQ onto the rails.

For now, though, I am still partial to the idea of changing my tables around. Mainly to spare my poor fingers from having to type out stuff like intRelationValueRelation when building queries. relation_id makes so much more sense.

Decisions decisions.

Using SQL Server views as Rails "tables"

In an effort to get a feel for the effort it’ll take to rewrite BiQ, I’ve been rewriting a vertical slice of our functionality. As part of that I’ve been setting up a test Ruby on Rails application and building a test suite for it.

One pretty important piece of the puzzle is our database. Over the years it has changed from the realm of an application database into being an integration database. Part of the rewrite is my desire to revert that.

I really want to change our database structure around so it matches the conventions of Rails. I believe the cost of doing so is by far outweighed by the benefits, especially since I’d have to map everything to some other name in our models otherwise.

tblUser.strUserFirstname is probably one of the worst naming schemes I’ve ever come up with. Having to create attribute accessors for everything isn’t going to make me like it a single bit more.

For now, while we’re exploring and testing, it will probably be most beneficial to preserve the old naming scheme. That way we can run tests and compare results on both the ASP/VBScript and the Rails application. Also we have scripts and stored procedures outside the ASP/VBScript application expecting the old structure that won’t be part of the initial rewrite.

SQL Server saves

My initial idea was to create simple views that create a 1-1 mapping of each table, renaming the columns as I see ie:

CREATE VIEW users AS SELECT intUserId AS id FROM tblUser).

This idea is great in all it’s simplicity because SQL Server makes this kind of views updatable. “Jakob”, I thought to myself, “you’re a genious”. Then I ran my tests and errors were everywhere.

ActiveRecord objects to my idea

For test fixtures to work in a predictable way you need to be able to insert into their id columns. To enable that on a table in SQL Server you do set identity_insert tableName on. However, Rails expects my table to be named users, but the database object by that name is really a view, hence I get the error:

ActiveRecord::ActiveRecordError: IDENTITY_INSERT could not be turned ON

Which in reality masks this error:

‘users’ is not a user table. Cannot perform SET operation.

Bummer. It was a good idea, though.

Create a trigger on the view to turn identity insert on

Creating an Instead-Of-Trigger that turns identity insert on is a fairly clean solution. It doesn’t work, though. The SQL Adapter always check if the insertion has an id column and if it has tries to turn identity insertion on. This results in the above error, since it uses the view name.

Hacking Fixtures

The above behaviour turned me onto something, though. There is a single method that handles turning identity insertion on and off: ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_identity_insert. Bingo! There’s my attack vector. I added the following ugly-as-heck-hack to test/test_helper and everything works:

module ActiveRecord
  module ConnectionAdapters
    class SQLServerAdapter < AbstractAdapter
      def enable_identity_insert(table_name, enable = true)
        if has_identity_column(table_name)
          "SET IDENTITY_INSERT tbl#{table_name.singularize} #{enable ? 'ON' : 'OFF'}"

At least our current database conventions are consistent enough for the above to work. I should probably reconsider my choice of not wanting to change the tables around, but this hack will serve me well until we move ahead with the actual rewrite.

Running Rails tests on SQL Server

Quick tip: If you want to run your Rails test suite through Rake on Windows using SQL Server, you need to have the scptxfr.exe utility in your path. On our development server, which is a pretty standard Microsoft SQL Server 2000 install, it’s located in C:\Program Files\Microsoft SQL Server\MSSQL\Upgrade.

And for the sake of completness, on Windows 2000 you change your path variable on My Computer > Properties > Advanced > Environment variables > System Variables. Only command prompts created after the fact will reflect that change.

Making CodeHighlighter play nice

I want snippets of code that I publish here on Mentalized to have fancy colors and syntax highlighting. It makes reading code easier in my editors and it should make it easier here as well. Enter CodeHighlighter.

a lightweight, unobstrusive and fully configurable script for displaying code examples highlighted in a way similar to the way many text editors highlight code.

Sure sounds good, too bad it sent my Firefox spinning into a dark grave of recursion whenever it was run on a document that also included Prototype – like, oh say, this weblog. Time to roll up those sleeves and get with the javascript.debugging.

When numbers aren't numbers

Today, on #rubyonrails, a stubborn developer insisted on storing phone numbers as integers. The rest of the channel tried to persuade him/her that this was a really, really bad idea. Part of the conversation went:

<Cyanshade> you assume that all phones are in the US?
<stubborn_dev_02> yes
<Cyanshade> my phone number is in the form +358 44 123 4567
<stubborn_dev_02> then i’d ban you

(Name has been changed to protect the innocent)

Kill the Rails pluralization

One of the oft-ranted about features of Rails, is the Inflector – also known as that f***** pluralizer. To turn it off, put this in your config/environment.rb, below the line that says “Include your application configuration below”:

ActiveRecord::Base.pluralize_table_names = false

This works in Rails 1.0, and counters seemingly weird issues where Rails can’t find your tables (like the “Before updating scaffolding from new DB schema, try creating a table for your model” message from the scaffold generator).

To figure out what the pluralizer does and expects, do check out Geoffrey Grosenbach’s Pluralizer Tool.

no such file to load -- mkmf

While checking out Scott Baron’s coverage-tool for Ruby, insurance, I ran into a minor issue on my - by now - fairly beat up Debian installation.

jcop@mental:~$ sudo gem install insurance
Attempting local installation of 'insurance'
Local gem file not found: insurance*.gem
Attempting remote installation of 'insurance'
Updating Gem source index for:
Building native extensions.  This could take a while...
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
        from extconf.rb:1

Thankfully, Google led me to the answer on RubyForge. For some reason, mkmf.rb is part of the ruby1.8-dev package, and initially I hadn’t installed that since it is described as

Header files for compiling extension modules for the Ruby 1.8

Ah well. A quick

sudo apt-get install ruby1.8-dev

and everything trotted along happily after that.

Announcing WoW Snitch

WoW Snitch (now disbanded) is a small application of mine that eases the pain of uploading data from your World of Warcraft AddOns to multiple websites.

If you know what Matt Millers fine UniUploader is and does, you can think of WoW Snitch as multiple UniUploaders in one application. Each task you create is more or less able to do what UniUploader does.

I personally use WoW Snitch to upload data to my guilds Guildspace website, Census+ data for Warcraft Census (mmm, stats), and for keeping my character profiles on RPG Outfitter up to date. Unfortunatly it’s currently not possible to upload to Allakhazam, Thottbot, or any of the other sites that employ their own, closed uploading mechanisms.

WoW snitch is the first Windows desktop development I have done for ages. It is a nice break away from the dynamically typed scripting languages I usually work with. There’s something soothing about that compile phase (probably because I haven’t reached 30 minute builds yet), and I really do like the warnings and errors a compiler can catch. That said, I definatly don’t mind giving up that in favor of the things made easy by for example Rubys ducktyping.

Having previously ranted about Windows applications and raved about OS X ditto, I sure hope I have managed to add some of the sexyness that’s so badly missing in many Windows applications to WoW Snitch. ::crosses fingers::

Head on over to the WoW Snitch website, download the snitch directly, and drop me some feedback in a mail or the forums or here, thanks.

Setting the request content type in Rails

To set the content type and character set of your responses of your Rails application, add this to your Application Controller (/app/controllers/application.rb):

class ApplicationController < ActionController::Base
  before_filter :set_content_type
  def set_content_type
    @headers["Content-Type"] = "text/html; charset=utf-8"

This can obviously be set in any other controller if you need special contenttypes, for example for a feeds controller:

class FeedsController < ApplicationController
  before_filter :set_content_type
  def set_content_type
    @headers["Content-Type"] = "text/xml; charset=utf-8"

How to stay on Rails 0.13.1

In these times where Rails 1.0 Release Candidates introduce memory leaks, bugs, and whatnot, you might want to lock your Rails application to a tried and tested version.

In your config/environment.rb, change your require_gem lines from

require 'active_support'
require 'active_record'
require 'action_controller'
require 'action_mailer'
require 'action_web_service'


require_gem 'activesupport', '= 1.1.1'
require_gem 'activerecord', '= 1.11.1'
require_gem 'actionpack', '= 1.9.1'
require_gem 'actionmailer', '= 1.0.1'
require_gem 'actionwebservice', '= 0.8.1'
require_gem 'rails', '= 0.13.1'

and you’ll be rolling on Rails 0.13.1 until you decide not to. Best way to make sure breakage doesn’t happen when your host decides to upgrade without warning you.

My stages of Ruby on Rails acceptance

Andy Budd has posted a about the stages of technological acceptance. They seem to mirror my acceptance of Ruby and Rails quite well.

I remember back when David showed me Ruby for the first time. Come to think of it, he was probably showing me Rails, but I was blissfully unaware of this. David was as excited and enthusiastic as a kid in a candystore, going “Look! You can do this! Open declarations! Ducktyping! Full Object Orientation! Blocks!”.

I looked at him going “Yes, that’s all very fine, but I can do websites in PHP or ASP too, what’s the big deal?”. Heh, kinda fun thinking about that. Obvious denial, eh?

Next phase is supposed to be anger. I don’t recall actually going through anything close to that. My anger phase came as part of my acceptance phase, while trying to install Ruby and Rails on Debian

My acceptance phase came when I kept hearing David rave about Rails and seeing him and everybody else release one kickass application after the other. I figured I’d look into it. It was about time to learn a new language anyways, and it couldn’t hurt.

From that point, understanding wasn’t far away. I think my Point of Understanding was right after Routes was added to Rails. I changed a route around and blam – all my links changed accordingly. I swear, I was clapping my hands with glee.

Leading to enthusiasm, which is definatly the phase I am still in. I am sure everyone in my nearest surroundings can attest to this. And I imagine this very weblog shows proof of this too.

I do wonder, though, what’s the next stage? Saturation? Boredom? Ignorant Bliss?


This little Ruby on Rails snippet solves a problem I’ve seen some people get confused by. I figured I’d share my way of doing it.

The trick to behind creating a has_and_belongs_to_many association that associates rows in the same table is to specify more options than you’re used to. Rails can’t figure out how to work it’s magic when associationg 2 classes named the same, so you have to guide it by hand. From a current project in the works:

class ClassDescription < Description
  has_and_belongs_to_many :included_modules, :class_name => 'ModuleDescription', :join_table => 'includes_modules', :foreign_key => 'description_id', :association_foreign_key => 'module_id'

Note that this also adds Single Table Inheritance (STI) into the mix, which makes the above less clear. The simplest thing you have to do is:

has_and_belongs_to_many :models, :association_foreign_key => 'other_model_id'

You may want to specify more options to make your tables more readable. The above assumes a table called model_model with the fields model_id and other_model_id.

Respect my table name prefix

What’s wrong with this line of Ruby on Rails code?

Car.find(:all, :conditions => ['cars.year = ?', @year])

If you’re doing your own, internal application, nothing major is wrong. However if your application is meant for distribution and end-user-deployment, you’re making dangerous assumptions about the database by hardcoding the table name.

Readable Rails code

Man, I love how humanly readable Ruby on Rails code can be. I mean, I reckon even my mother could guess what this line of actual code does:

redirect_to login_url unless current_user.has_permission_to(MANAGE_USERS)

MySQL Error 12 from table handler

Lately I’ve been getting the occasional “Error 12 from table handler” from the MySQL server on my latest pet project. It’s only certain queries that fail, but when it has failed once it seemingly keeps failing until the MySQL server is bounced.

Apparently for a query to fail, all it needs is to be a join with two tables with the result ordered. Ie

SELECT * FROM a, b WHERE = b.join_id ORDER BY

Removing the “order by” clause makes the error go away.

I have no idea what the error means, apart from that it apparently has something to do with memory. The two tables in question have 5 and 40 rows respectivly, so it’s not like it’s a big resultset to order.

Switching my table type from MyISAM to INNO DB seemed to make the errors go away – at least for now…

Update: It seems that this might’ve been a bug in MySQL v4.0something, at least that’s what my host have informed me. Here’s hoping for a repair/upgrade soon’ish.

Ruby on Rails hurts developer productivity

Gah, using Ruby on Rails is seriously influencing my productivity at work in a negative way. I take longer doing simple stuff, my code has more errors and never works the first time anymore.

“Why? How? Everybody else praise Rails to the skies, why are you different, Jakob?”, I hear the masses cry out. I’ll tell you why!

After having gotten used to RoR for a private project or 2, all the web development stuff I do at work feels ugly, clunky and unproductive, simply because I am not using Ruby on Rails for it.

Now my if statements are all wrong, I try to create objects that don’t exist, I think in terms of before- and after-filters, I expect everything handed to me on a gold platter, but it just doesn’t happen like it does in RoR!

So take my advice: If you are stuck doing legacy work on a legacy system, don’t think about learning RoR – you might never want to go back to work if you do.

Retrofitting unit tests

I spent a good deal of yesterday adding testcases to the recently added unittests on BiQ.

I really like the way unit testing invites me to break my code by coming up with unhandled cases. I basically went through my functions, reading the descriptions I made ages ago, followed by me hatching evil plans for torturing my code (“How about I pass you an object instead of a string, you pesky function, how’d you like that, eh? EH?”).

And it worked.

I found and fixed a bunch of minor bugs that simply didn’t get exposed in our current codebase. It’s likely they would never have been until we decide to use the function in some obscure, unexpected way months from now when I’ve forgot how the function was supposed to work.

I still have ways to go before I get into a real test-driven development state of mind, though. I guess I should re-read the book now I’ve actually got some hands-on experience with this.

Proverbs in code

English proverbs represented as code. If you can guess what they are, I must have made them at least somewhat correct:

if ( != book.cover) {
if chickens.state == "hatched" {
  return chickens.count;
if (bodyPart.isClass(Hand)) {
  if (bodyPart != self.feeder) {
while true {
def understandRecursion():
while (self.proficiency(skill) < masterLevel) {
if count = 2 then
  result := "company"
else if count = 3
  result := "crowd";

Developer defenses

Being a wee, humble programmer working on a comparativly small project like BiQ, it is somehow comforting to read a Blizzard programmer publically state about a feature he did on World of Warcraft:

[…]I had to implement that. I probably screwed it up somehow[…]

Makes me feel not so bad for introducing the bugs I do, and it’s an archtypical example of the defenses developers usually set up when talking about their works.

F5 - The Wonder Key

I’ve had the pleasure of working on a part of our website that my boss was acceptance testing

We had a session today at work where my boss was acceptance testing part of our website while I was working on it. Whenever I introduced a bug or a premature save caused syntax error he would notice it within a minute. At the point he noticed the error I would usually already have seen and corrected it, so our conversations for half the day went like


“Jakob, there’s an error!”

“Yeah, press F5”

“It’s gone!”

“I know”


I present to you F5 – The wonder key that enables bosses to fix every single bug with a single keypress.

Living with design decisions

Once in a while on a project design phase, you make a decision based on all the right reasons which later turns out to be a stupid decision. In some cases you won’t realize the decision was stupid until it has hooked its claws into every aspect of your design and code. By that time it is often too much work trying to unhook it.

That’s the situation I am in. So do I start doing things the proper way, thereby introducing inconsistencies in the naming policy, or do I suck it up and stick to the bad design decision made months ago, keeping the names consistent throughout the program?

I decided to go the inconsistent way in a new feature as an experiment. I figure this is the only way to ever figure out if the original decision actually was as stupid as I think. Besides it should be relatively easy to revert just this single, new part of the code if it turns out that I don’t like the way this experiment goes.

Selfconfusing code

Sigh, going through one of our more complex, rarely edited sourcefiles at work, I came across this line:


I have a reasonably good grasp of what i is. j seems to have very little relation to anything, and I have absolutely no clue what the 19 is. 19 what?

Developers out there, for my sake, if you don’t already, please start using meaningful variable names, and hey, I wouldn’t mind a few constants on occasion.

Top 3 missing VBScript features

Why oh why am I stuck developing ASP/VBScript – that language is so crippled I can’t believe it. Every time I try to do something Right™ I bang my head against silly limitations of the language. At the moment I am really really missing

  1. Proper including of external files. Server Side Includes and/or Server.Execute doesn’t quite cut it when the scripting language doesn’t have namespaces or any way of checking if a certain class already exists. If you have defined one name somewhere in a file, the language provides you with no way to avoid including that file a second time elsewhere in your code, which results in a compilation error.
  2. Proper objects. Currently there is no inheritance, no typechecking of objects, no polymorphism. Objects are nothing more than a way to group functions. Actually they might be the a way to provide namespaces if it was possible to avoid including files more than once.
  3. Crappy error handling. I want exceptions. Just telling the scripting engine to continue to next line if an error occurs doesn’t cut it.

Hm, I wonder if it is possible to migrate seamlessly to a .NET solution without having to rewrite the entire codebase at once.

Indentation intents

Gah, I just spent the last hour on the phone with a developer trying to debug a Python script, that had to run in our enviroment and couldn’t be debugged by him locally. Long distance eXtreme Programming I suppose.

We found the bug after nearly tearing eachothers hair out. He had indented a line using 2 tabs instead the one tab and 4 spaces the rest of the block was indented with. Unfortunatly this hits me right in the What-I-Don’t-Like-About-Python area (and Python-style indentation is probably the first thing that new users stumble over).

String concatenation in VBScript

While developing a template parser to replace our existing template-object at work, it finally dawned on me how insanely slow ASP/VBScripts standard string concatenation actually is. It isn’t something you’d notice in everyday development, but when you have to concatenate big strings VBScript almost grinds to a halt.

When generating HTML pages from existing templatefiles, you do a lot of putting strings together and the initial revision of my class was extremly slow when compared to our existing template-object. Investigating the execution times of individual parts of my parser revealed that the culprit was a single line concatenating 2 strings which gradually increase in size.

When in distress it’s a good thing to be able to rely on ones friends, so I turned to my old pal, Google. He pointed me to 2 classes dealing with this exact problem: StringBuilder and FastString.

Both classes use arrays to speed up the process of concatenating strings and boy, do they go faster than VBScript does on its own. The speed increase is massive (some of my tests ran 50 times quicker). The two implementations are very alike in use and speedwise they are nearly similar, with StringBuilder being vaguely faster (a few tenths of a second when doing 50000 concatenations). The end result is that my template parser now runs with nearly the same speed as our old template COM object, Sumatra Templates, and when parsing big templates it’s noticable faster.

I think I will consider twice the next time I’m about to do a standard VBScript concatenation.

<a target="bad idea">

Or “Why forcing links into a new window is bad”

I realize the topic of opening links in new windows has been covered countless times, but the dead horse has shown its ugly head and I am intent on beating it down – using this blog and your valuable time to organize my thoughts, my apologies.

The major bad issues with opening links in a new window as I see them:

  • Removes control from the user
  • Breaks standard behaviour
  • Breaks the backbutton
  • There is no reason

(Note, this is directed at people creating websites, not you as a surfing user. If you feel like opening a link in a new window, by all means do – that’s pretty much the point.)

Microsoft patenting APIs

The barely noticable, yet significant alarms in the back of my head are going off again – The RidiculousPatent-sense is tingling:

An application program interface (API) provides a set of functions for application developers who build Web applications on Microsoft Corporation’s .NET.TM. platform

That’s the abstract from a patent filed by Microsoft. It is no surprise that Microsoft wants to control the use of their .net technology seeing how they’ve spent a lot of time and money on it, but to my untrained eye (I go all crosseyed and my brain starts shutting down when I try to read patent claims) it looks as they are trying control the concept of writing APIs to access web services.

Surely, that claim is so broad and with so many examples of prior art that it won’t stand against any patent infringement claims. APIs are vital to the ever-confusing world of programming, having them wrap around client/server communication is by no means a new idea. Someone claiming and enforcing a patent on APIs would be devastating.

Even if this patent only applies to the libraries/APIs that ship with the .Net Framework the effects are – perhaps not devasting, but at least very discouraging. In that case it will effectivly allow the Redmond BigCo to control what platforms they want interfacing the .net technology.

Honest schedules

I admit, I have commited one of the death-sins of project development. I let myself to believe, that a tight schedule and increased working hours would enable me to fulfill the deadline my boss had set for a project.

As a result I am now grossly overdue, I am feeling ever more demotivated and I am constantly walking around with a gnawing feeling in my stomach telling me that I am too stressed and that I haven’t been doing my job well enough.

Trust me on this one, don’t fool yourself: Make an honest schedule, the alternative simply isn’t worth it.