Now blogging at Reevoo Labs

Just a short update to announce that I’m an author on the Reevoo labs blog, and my first article about unit testing Apache redirect rules using Test::Unit is now live.

Labs is a place where us folks at Reevoo can talk about cool stuff that we are doing, or announce the random crap that we get up to on thursday afternoons. We haven’t been using it anywhere near as much as we should and as of now that’s going to change!

As a general rule for the future anything I work on during business hours will be posted at the labs site, and anything about my personal code and random blurbs will remain here so stay tuned.


Always Learning! Enumerable#inject and Hash#merge in Ruby

I learned something cool today. While discussing neat one liners in Ruby, someone suggested a potential challenge, it went something like this:

Given an array of numbers, how would you convert that into a hash where the key is the number and the value is the number of times that it appears in the array. so for example you start with the array

1 
2 [1,1,2,3,2,4,5,3,3,3,1,1,2,6,5,5,5,5]
3 

And what you want to end up with is something like this:

1 
2 {5=>5, 6=>1, 1=>4, 2=>3, 3=>4, 4=>1}
3 

So how would you do it? The solution I came up with after a couple of minutes was this:

1 
2 h = Hash.new(0)
3 [1,1,2,3,2,4,5,3,3,3,1,1,2,6,5,5,5,5].each { |i| h[i] += 1 }
4 puts h
5 

This is a fairly simple solution, first you create a new hash and initialise every value with 0, then we iterate over each value of the array, passing the value to a block which finds the hash entry with the correct key and increments its value (this is acheivable because we initialise our hash with a Fixnum as a default value). I thought this was pretty neat but it turns out, as with most things in Ruby, that there is a much cooler way :)

It looks like this:

1 
2 p [1,1,2,3,2,4,5,3,3,3,1,1,2,6,5,5,5,5].inject(Hash.new(0)){ |memo,i| memo.merge({i => memo[i] += 1}) } 
3 

So how is this working?

The first thing we need to look at is Enumerable#inject. This takes an argument and a block. Inject then calls the block once for every element in the Collection we are calling it for, in this case this is our array of values. The argument we give to the inject function will be yielded as the first argument to the block (memo), the second argument in the block(i) is the value in our collection we are working on.

An important thing to note with inject is that the return value of the block is passed into the block on the subsequent call, replacing the old value of our first block argument (memo in this case).

This lets us do something funky inside the block using the Hash#merge function. merge takes a hash as an argument and merges it’s values into the hash that it’s called on. A couple of examples to illustrate this (in irb):

1 
2 irb(main):007:0> {"first" => 2}.merge({"second" => 2})
3 => {"second"=>2, "first"=>2}
4 irb(main):001:0> {"first" => 2}.merge({"first" => 4})
5 => {"first"=>4}
6 

So what is our merge function doing? It’s passing in an empty hash initialised with zeros, and for every entry in the array it’s merging in a new hash that just contains the incremented value for that particular key.

The p at the beginning is just a shorthand way of saying puts, so that our final hash is printed to the terminal

Clever huh?


Rails Development with Ubuntu

Having recently purchased myself a new Dell Mini 9, I got a chance to set up a development environment for rails apps so that I can hack on the train to and from work. It’s not particularly hard but I thought I’d document it for reference and hopefully someone will find this useful.

First of all a note or 2 about the hardware, the machine is a 1.6GHz Intel Atom chip. A single core, cpu that supports hyper-threading (it shows up in /proc/cpuinfo as 2 seperate cpu’s but you can only control scaling on cpu0). It has a 4Gb SSD and 1Gb of RAM. This minimal spec turns out to be more than enough for a full Rails/mysql/mongrel development stack if you’re careful with a few defaults.

First of all I removed all unnecesary packages using a combination of apt-get and synaptic. apt-get remove is useful if you know what you want to kill (such as openoffice.org) but I find synaptic really useful for browsing through stuff you’re not sure about, libraries for example, and killing anything that doesn’t sound useful.

The next step is to install ruby, rubygems and a few useful dependancies. This is covered in detail elsewhere on the web so I’ll be brief, basically just:

1 sudo apt-get install ruby1.8 irb1.8 rdoc1.8 ri1.8 \
2              readline build-essential libopenssl-ruby1.8
3 sudo ln -s /usr/bin/ruby /usr/bin/ruby1.8 #repeat for irb,ri and rdoc
4 wget http://www.rubygems.org/rubygems/rubygems-1.3.1.tar.gz
5 tar zxvf rubygems-1.3.1.tar.gz && cd rubygems-1.3.1
6 sudo ruby setup.rb

If you’d like a nice deb package that you can track with apt, you can replace the last step with:

1 sudo apt-get install checkinstall
2 checkinstall 'sudo ruby setup.rb'

Checkinstall is an awesome piece of software, it’s so awesome it deserves it’s own post so I’ll skip over the details, check it out.

After setting up this step you should now have a fully functioning rubygems installation, test it by issuing gem -v

Next step is to get mysql installed and configured. I did this using apt-get, because it’s easy:

1 sudo apt-get install mysql-server mysql-client libmysql-dev

Be aware that you need the mysql development headers in order to compile the mysql gem, which we will install next, alongside adding Github to our gems sources, installing rails and a bunch of other useful stuff.

1 sudo gem sources -a http://gems.github.com
2 sudo gem install rails mysql sqlite3-ruby capistrano hoe hpricot webby RedCloth rake

And that is pretty much all there is to it. you can now run your favourite editor (in my case gedit) and start writing Ruby.

There are a couple of Dell Mini specific issues that I had to do before I found this machine to be a truly comfortable development environment though.

  1. Remove the bottom Gnome taskbar, and make the fonts and theme stuff as small as possible, 1024×600 is a pretty tight screen resolution for web stuff, so it’s good to make the most of it. I use a combination of 8 pt Liberation Sans and Mono for my desktop/coding fonts, at 96dpi with subpixel smoothing, and slight hinting. Clearlooks Compact GTK theme, Metabox for Metacity and the GNOME-Brave icon set.
  2. Slow the keyboard repeat rate down. The keyboard on the mini 9 is small, it’s reasonable easy to get used to but it is exceptionally sensitive by default to repeated keypresses, which are hard to avoid on a keyboard this small. I turned my repeat rate up to it’s longest setting in Gnome Keyboard preferences and the problem has pretty much gone away.
  3. Turn off the trackpad while typing. This gets very annoying very quickly, the trackpad is pretty sensitive and due to the size of the whole unit, you can very easily get in the way of yourself while typing, especially for longer stints (like this blog post) thankfully the default synaptic driver handles this by default, you just have to enable the daemon. To do this either:
  • run syndaemon -d -t in a terminal to make it active per session, or
  • add an entry to Gnome → Preferences → Sessions to make it run on login.

That’s pretty much everything I’ve done so far. If I find anything else that works really well I’ll update this post. Also any comments/suggestions then email them to me :)


Thoughts on the Windows 7 beta

I downloaded the new Windows 7 beta a couple of weekends ago, and test drove it on a laptop I borrowed from work. These are the notes that I made while playing with it for an hour or so on the train to work (bad spelling and grammar included).

Bear in mind that I’m predominantly a UNIX geek who has only used Vista briefly, and only uses XP for light gaming.

Hardware tested on was an HP 2510p laptop with a 1.2GHz Core 2 Duo processor and 2Gb of RAM, it contains an Intel 915 video card and the Intel 4965 Wireless chipset (making it officially a Centrino pro platform).

Windows7 thoughts:

  • Installation is pretty much the same as Vista, boot times and shut down times seem quicker, although I have nothing to compare against, I’ve never seen this hardware running Vista. suspend and resume is ninja fast, it’s almost got Apple beat.
  • Interface feels much snappier than Vista, windows take less time to open and respond quicker to mouse movement etc.
  • Start menu redesign, I like the recent files list, that’s specific to each app and that it’s less ostentatious overall than Vista’s
  • Inclusion of PowerShell is probably a good thing. Would like to spend more time playing with it.
  • Libraries! basically a view over all the files of a certain type in multiple directories. eg. Music library aggregates all mp3 files in your Music folder and the Public music folder by default: You can create and customize views but options are limited at the moment, worth keeping an eye on though.
  • New taskbar: very different from old taskbar design, Quick launch and tasks now effectively merged into each other much easier to see what you’re using, also maximises taskbar real estate. Coupled with an effective preview system for when you hover over each taskbar icon this pretty much works!
  • Enhanced Log Viewer, which I like. Have a funny feeling that this was in Vista, but I might have missed it
  • Awesome power management settings, including control over CPU speed in different circumstances, and the ability to create your own custom power plans rather than just being able to make minor tweaks to existing plans. Don’t know if this was in Vista either.
  • Notepad is still shit. Come on people (and this goes for Apple too) what would is take to give us a decent text editor out of the box?
  • Start menu says shutdown by default now instead of the annoying suspend issue.
  • As far as I can tell there is no way to adjust the windows Aero settings from the right click display menu. You need to dig around in performance settings, seems a little backward.
  • Copying files is much speedier, especially the calculating stage before copy, only took ~5 seconds to recognise 41000 files totalling 6Gb (tested copying the contents of C:\Windows to a location in my home directory)
  • I like the new Play – Burn – Sync tabbed layout in Media Player, although haven’t loaded any music into it so have no idea how it performs. also untested with my Samsung KP-T10 (an MTP device)
  • And finally (this is the most awesome thing). They made Paint useful, I can now crop images and actually use it for basic image editing. Also Paint now saves in png by default which is very nice.

Overall, it’s pretty cool. I like it. Hopefully when it comes out I’ll have some good news when my relatives ask me if it’s safe to upgrade from XP yet!


Using Webby

Recently I found the time to give theshadowaspect.com a much needed redesign and in the process decided to switch systems from the hybrid wordpress/custom thing that I was using before. Seeing as I am a sysadmin by trade working in a Ruby on Rails shop (reevoo.com for those that are interested) I wanted to use something Ruby based. Unfortunately my options were severly limited by the hosting environment that I am using.

Rails and their ilk were out of the question, seeing as my shared hosting account doesn’t support it and doesn’t look like it’s going to anytime soon. I don’t have ssh access, so installing Passenger wasn’t feasible, and in fact my only access to the account is using either ftp or the cpanel frontend that is provided.

I decided that what I needed was a static site, but one that was easy to manage and using open source tools. I also wanted to avoid php as, being frank, I’ve had my fill of php blogging engines.

I looked briefly at thingamablog, which is very cool. It’s a multi-platform app (Java based), that lets you configure your site and write posts on your local machine using it’s GUI, and when you are done you simply hit publish and it builds your static site, and plops it into an output directory ready for uploading to a web server. It works very well, but I wanted something Ruby based.

Due to the praise that some of my work colleagues had been heaping on it I decided to give Webby a go. It doesn’t dissapoint.

Webby is basically some advanced Rake scripts that build a template for your site which you can then edit to your hearts desire, when you are done you run the build command and it creates your site and puts it in the output directory ready for upload. It’s a similar concept to thingamablog, but it’s done in the Ruby Way ;)

the basic workflow in webby is as follows:

  • Use webby-gen to build the template for your site, there are several build in templates depending on what you want to do like website, blog, tumblog etc.
1 webby-gen website test

This will output something similar to the following:

 1 create  test
 2 create  test/content
 3 create  test/content/css
 4 create  test/content/css/blueprint
 5 create  test/content/css/blueprint/compressed
 6 create  test/content/css/blueprint/lib
 7 create  test/content/css/blueprint/plugins
 8 create  test/content/css/blueprint/plugins/buttons
 9 create  test/content/css/blueprint/plugins/buttons/icons
10 create  test/content/css/blueprint/plugins/css-classes
11 create  test/content/css/blueprint/plugins/fancy-type
12 create  test/layouts
13 create ...
  • Edit the site as you wish: pages, images and css are placed in the content directory, the layout directory contains all of your basic templates and files that define the markup for the site, these are all erb files so you can write Ruby code to do cool stuff in them. Templates for things like auto generation of partials and rss feeds are all found in the templates directory. You can write Ruby scripts for advanced content and dump them in lib.

One cool thing about webby is that the actual content for the pages and the posts can be written in several ways, you can write straight html if that’s your thing, and there are also filters for erb and textile, so you can concentrate on writing without having crap loads of markup all over the place!

  • Build the site. This is as simple as doing:
1 webby build

from your sites root directory, and will dump all the html,css, images etc into a folder called output which can then be copied to your server and you’re away.

Aside from this basic workflow there are a load of other notable points that deserve a mention.

Firstly the autobuild task. Webby builds your site as normal but also starts up a webbrick server attached to port 4331, it also fires up your web browser pointing to that address. The principle being that whenever you save a file, webby automatically rebuilds it and updates the site so you can develop your site without having to keep rebuilding it all the time.

Secondly, webby has built in tasks for verifying all the external and internal links on your site. So no more worrying about having broken links on the site.

Thirdly, don’t even want to ftp your site manually, webby has built-in deploy tasks so you can autmatically build and deploy your site (using scp or rsync) without ever having to leave your terminal.

There are loads more things that I could write about, such as CodeRay support (for pretty marking up and syntax highlighting of a bunch of popular languages) and partials, but this is going to go on forever so I’ll leave you to discover them for yourself.

I realise this article is basically just a pimp webby session, but damn I’m so impressed I feel the need to. Additionally the entire webby directory for this site is available from my github repository, so feel free to tear it down and work out how everything works.


Fixing font rendering in Arch/Gnome

If you use Arch linux you may have noticed that the default font rendering in Gnome is a bit lacking, especially if you don’t have Xfce installed as well, this appears to be because Xfce installs gsfonts as one of it’s dependancies yet Gnome doesn’t. So the first to do is install it.

$ pacman -S gsfonts

The next thing I like to do is to install the liberation font set, and the microsoft web fonts. These are available using pacman and can be installed as follows:

1 $ pacman -S ttf-liberation ttf-ms-fonts

Then I like to install the versions of freetype, libxft and cairo with the cleartype patches applied. These are available from AUR, so grab them and install them. If you have yaourt you can simply do

1 yaourt -S cairo-cleartype freetype2-cleartype libxft-cleartype

Then install the packages that it creates (usually in /tmp/yaourt somehwhere). NB: Make sure that if you have installed any of the non cleartype versions of these packages using pacman that you remove them before installing the new versions.
If you don’t have yaourt installed for whatever reason you can grab the tarballs directly from the AUR repo and build the packages like this:

1 $ tar zxvf package.src.tar.gz
2 $ cd package
3 $ makepkg
4 $ pacman -U package-i686.pkg.tar.gz

The remaining step is to configure your font preferences in the Gnome Appearance dialog, my preferred settings are:


  • Resolution 96dpi
  • Subpixel (LCD) smoothing
  • Slight Hinting
  • RGB Sub-pixel order

Now you should be able to restart X and enjoy some much better looking fonts


About the author

shadowaspect's avatar

My name is Matt House and I’m a sysadmin at Reevoo, a purveyor of the finest impartial reviews and product comparisons. There I deploy and maintain Rails apps on Linux and OSX machines using Ruby, Rails, MySQL, Capistrano, Puppet, Apache, Mongrel and Passenger, amongst others.

I’m not looking for work at the moment but if you wish to contact me for any other reason you can email me (matt at theshadowaspect.com), or send @shadowaspect a direct message on twitter.

About the site

theshadowaspect.com was written using Webby, a (very small) Ruby CMS. It is © Matt House 2009 and cheerfully untested in all variants of Internet Explorer. Do yourself a favour and Get Firefox

The design was heavily influenced by The Redmine bug tracker for the Ruby language. Thanks folks!

Posts