- Posted by liammclennan on June 23, 2010
Here are the original mockups for KeyRef


and the implemented screens look like (apologies for the ugliness):


both the application search, and the shortcut search, are working.
I tweaked the application shortcuts page url, so now the url for visual studio shortcuts is:
http://localhost:3000/app/show/4c19e8a5cfbfb01794000001/Visual%20Studio-shortcuts
Also, I have a page for entering new shortcuts that looks like:

Now I can start adding shortcuts, which will be a big job. I’d like to get it online so that people can add shortcuts for their favourite applications.
- Posted by liammclennan on June 17, 2010
Just because I like to build things, and I like to learn, I have been working on a keyboard shortcut reference site. I am using this as an opportunity to improve my ruby and rails skills.
The first few days were frustrating. Perhaps the learning curve of all the fun new toys was a bit excessive. Finally tonight things have really started to come together. I still don’t understand the rails built-in testing support but I will get there.
Interesting Things I Learned Tonight
RubyMine IDE
Tonight I switched to RubyMine instead of my usual Notepad++. I suspect RubyMine is a powerful tool if you know how to use it – but I don’t. At the moment it gives me errors about some gems not being activated. This is another one of those things that I will get to. I have also noticed that the editor functions significantly differently to the editors I am used to. For example, in visual studio and notepad++ if you place the cursor at the start of a line and press left arrow the cursor is sent to the end of the previous line. In RubyMine nothing happens.

Haml
Haml is my favourite view engine. For my .NET work I have been using its non-union Mexican CLR equivalent – nHaml.
Multiple CSS Classes
To define a div with more than one css class haml lets you chain them together with a ‘.’, such as:
.span-6.search_result contents of the div go here

Indent Consistency
I also learnt tonight that both haml and nhaml complain if you are not consistent about indenting. As a consequence of the move from notepad++ to RubyMine my haml views ended up with some tab indenting and some space indenting. For the view to render all of the indents within a view must be consistent.
Sorting Arrays
I guessed that ruby would be able to sort an array alphabetically by a property of the elements so my first attempt was:
Application.all.sort {|app| app.name}
which does not work. You have to supply a comparer (much like .NET). The correct sort is:
Application.all.sort {|a,b| a.name.downcase <=> b.name.downcase}
MongoMapper Find by Id
Since document databases are just fancy key-value stores it is essential to be able to easily search for a document by its id. This functionality is so intrinsic that it seems that the MongoMapper author did not bother to document it. To search by id simply pass the id to the find method:
Application.find(‘4c19e8facfbfb01794000002’)
Rails And CoffeeScript
I am a big fan of CoffeeScript so integrating it into this application is high on my priorities. My first thought was to copy Dr Nic’s strategy. Unfortunately, I did not get past step 1. Install Node.js. I am doing my development on Windows and node is unix only. I looked around for a solution but eventually had to concede defeat… for now.
Quicksearch
The front page of the application I am building displays a list of applications.

When the user types in the search box I want to reduce the list of applications to match their search. A quick googlebing turned up quicksearch, a jquery plugin. You simply tell quicksearch where to get its input (the search textbox) and the list of items to filter (the divs containing the names of applications) and it just works. Here is the code:
$('#app_search').quicksearch('.search_result');
Summary
I have had a productive evening. The app now displays a list of applications, allows them to be sorted and links through to an application page when an application is selected. Next on the list is to display the set of keyboard shortcuts for an application.
- Posted by liammclennan on June 11, 2010
Rake is a ruby internal DSL for build scripting. With (or without) the help of albacore rake makes an excellent build scripting tool for .NET projects.
The albacore documentation does a good job of explaining how to build solutions with rake but there is nothing to assist with another common build task – updating configuration files.
The following ruby script provides some helper methods for performing common configuration changes that are required as part of a build process.
class ConfigTasks
def self.set_app_setting(config_file, key, value)
ovsd_element = config_file.root.elements['appSettings'].get_elements("add[@key='#{key}']")[0]
ovsd_element.attributes['value'] = value
end
def self.set_connection_string(config_file, name, connection_string)
conn_string_element = config_file.root.elements['connectionStrings'].get_elements("add[@name='#{name}']")[0]
conn_string_element.attributes['connectionString'] = connection_string
end
def self.set_debug_compilation(config_file, debug_compilation)
compilation_element = config_file.root.elements['system.web'].get_elements("compilation")[0]
compilation_element.attributes['debug'] = false
end
private
def self.write_xml_to_file(xml_document, file)
File.open(file, 'w') do |config_file|
formatter = REXML::Formatters::Default.new
formatter.write(xml_document, config_file)
end
end
end
To use, require the file and call the class methods, passing the configuration file name and any other parameters.
require 'config_tasks'
ConfigTasks.set_app_setting 'web.config', 'enableCache', 'false'
- Posted by liammclennan on May 24, 2010
According to Wikipedia, “why the lucky stiff was the persona of an anonymous, but prolific writer, cartoonist, musician, artist, and computer programmer”. He looks a bit like Jack Black.
His book, Why’s Poignant Guide to Ruby, is a classic, though it can be hard to find since Why disappeared. If you want to learn the Ruby programming language I highly recommend Why’s Poignant Guide to Ruby. I am including a link here so that others who search for it may find it more easily.
- Posted by liammclennan on March 23, 2010
My last post was about a class for building test data objects in C#. This post describes the same tool, but implemented in Ruby. The C# version was written first but I originally came up with the solution in my head using Ruby, and then I translated it to C#. The Ruby version was easier to write and is easier to use thanks to Ruby’s dynamic nature making generics unnecessary.
Here are my example domain classes:
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
class Property
attr_accessor :street, :manager
def initialize(street, manager)
@street = street
@manager = manager
end
end
and the test class showing what the builder does:
class Test_Builder < Test::Unit::TestCase
def setup
@build = Builder.new
@build.configure({
Property => lambda { Property.new '127 Creek St', @build.a(Person) },
Person => lambda { Person.new 'Liam', 26 }
})
end
def test_create
assert_not_nil @build
end
def test_can_get_a_person
@person = @build.a(Person)
assert_not_nil @person
assert_equal 'Liam', @person.name
assert_equal 26, @person.age
end
def test_can_get_a_modified_person
@person = @build.a Person do |person|
person.age = 999
end
assert_not_nil @person
assert_equal 'Liam', @person.name
assert_equal 999, @person.age
end
def test_can_get_a_different_type_that_depends_on_a_type_that_has_not_been_configured_yet
@my_place = @build.a(Property)
assert_not_nil @my_place
assert_equal '127 Creek St', @my_place.street
assert_equal @build.a(Person).name, @my_place.manager.name
end
end
Finally, the implementation of Builder:
class Builder
# defaults is a hash of Class => creation lambda
def configure defaults
@defaults = defaults
end
def a(klass)
temp = @defaults[klass].call()
yield temp if block_given?
temp
end
end
- Posted by liammclennan on March 20, 2010
Last week on Software Craftsman Pilgrimage I was trying to organise where I will be travelling, and the companies I will be pairing with.
I now have a confirmed itinerary.
| 9 - 11th April |
Alt.NET Seattle |
| 12th April |
Craftsman visit with Didit (Long Island) |
| 13th April |
rest day :) |
| 14th April |
Craftsman visit with Obtiva (Chicago) |
| 15th – 16th April |
Craftsman visit with 8th Light (Chicago) |
| 17th – 18th April |
Seattle Code Camp |
I am looking forward to all of my visits and talking to all the smart people who work there. I will be blogging my progress and hopefully shooting some video.
If you are in Seattle, New York or Chicago and would like to meet up to chat about craftsmanship, programming, ruby or .NET please email me.

- Posted by liammclennan on November 27, 2009
Javascript does not have classes in the traditional sense, but we can achieve something similar in a number of ways. C# and Ruby both have standard class syntax.
This post is part of a series comparing the language features of the C#, Javascript and Ruby programming languages.
C#
public class Vehicle
{
protected string Make { get; private set; }
protected string Model { get; private set; }
public Vehicle(string make, string model)
{
Make = make;
Model = model;
}
public virtual void Print()
{
Console.WriteLine(GetDescription());
}
protected string GetDescription()
{
// string formatting syntax return string.Format("Make: {0} Model: {1}", Make, Model);
}
}
public class Helicopter : Vehicle // inheritance syntax {
public int NumberOfRotorBlades { get; private set; }
public Helicopter(int numberOfRotorBlades, string make, string model)
: base(make, model)
{
NumberOfRotorBlades = numberOfRotorBlades;
}
public override void Print()
{
// string concatenation syntax Console.WriteLine(GetDescription() + " Number of Rotor Blades:" + NumberOfRotorBlades);
}
}
Javascript
Class:
// declare vehicle 'class' var vehicle = function(seed) {
var that = {};
// private method var getDescription = function() {
return "Make: " + seed.make + " Model: " + seed.model;
};
// public method that.print = function() {
alert(getDescription());
};
return that;
};
// instantiate a vehicle var magna = vehicle({
make: 'Mitsubishi',
model: 'Magna' }); magna.print();
Derived class:
// declare helicopter 'class' var helicopter = function(seed) {
var that = {};
that.prototype = vehicle(seed);
var getDescription = function() {
return "Make: " + seed.make + " Model: " + seed.model;
};
that.print = function() {
alert(getDescription() + " Number of Rotor Blades:" + seed.numberOfRotorBlades);
};
return that;
};
// instantiate a helicopter var ah64 = helicopter({
make: 'Hughes Helicopters',
model: 'AH-64',
numberOfRotorBlades: 4 }); ah64.print();
Ruby
class Vehicle def initialize(make, model) @make = make;
@model = model;
end
def print
puts get_description
end
private
def get_description
return "Make: #{@make} Model: #{@model}" end end magna = Vehicle.new('Mitsubishi', 'Magna')
magna.print
class Helicopter < Vehicle
def initialize(make, model, number_of_rotors)
super(make, model)
@number_of_rotors = number_of_rotors
end
def print
puts get_description + " Number of Rotors: #{@number_of_rotors}" end end ah64 = Helicopter.new("Hughes Helicopters", "AH-64", 4)
ah64.print
- Posted by liammclennan on November 24, 2009
This post is part of a series comparing the language features of the C#, Javascript and Ruby programming languages.
Variables
C# requires that variables be declared with a specific type. Javascript and Ruby determine the type of variables at runtime. Here is the syntax:
C#
public string publicMessage = "Hello World";
private string privateMessage = "Hello World";
static string PRIVATE_MESSAGE = "Hello World";
Javascript
var message = "Hello World";
Javascript does not have a syntax for making variables public or private, instead it is achieved by a clever usage of closure (discussed later). Javascript does not have block scope, which means that variables defined within a block can be accessed from outside the block (a block is anything in {} such as and if statement or a loop). In Javascript variables are scoped to the function in which they are declared and are visible anywhere within that function, including within inner functions.
// this function will alert "Hello World" twice.
function showMessage() {
if (true) {
var message = "Hello World";
}
alert(message);
var innerFunction = function() {
alert(message);
};
innerFunction();
}
Ruby
A ruby variable is declared by assigning to the variable name. Ruby uses prefixes to indicate the variable scope.
# local variable
message = "Hello World"
# instance variable
@message = "Hello World"
# static variable
@@message = "Hello World"
# global variable
$message = "Hello World"
- Posted by liammclennan on October 16, 2009
Executable specifications are the holy grail of software development. The idea is to specify the behaviour of a system in some structured way that can be automatically verified. It is something that I am interested in so I have been keeping an eye on the options that are available for .NET.
Cucumber with IronRuby
Cucumber is a popular ruby tool. Specifications are written as plain text files and made executable by writing regular expressions for each line of the text file. Cucumber has a strong BDD flavour, encouraging users to write their specifications in the given, when, then style. I have not tried it myself but I know a number of people have indicated that getting cucumber to work in ironruby is difficult.
NGourd
NGourd is a native .NET project inspired by Cucumber and written largely by Mike Minutillo. Again, regular expressions are used to extract the parameters from the specifications, but this time the regular expressions are written as attributes. I contributed some code to this project to log its the output in a format that can be used by build servers such as Team City and Cruise Control .NET. NGourd is rudimentary but it does work and I have used it in some of my projects. As a developer, this type of tool is my first choice. I like that the specifications are plain text files that can be easily processed and versioned. However, if the aim is to have non-programmers working on the specifications then I fear that plain text may not offer them enough support.
StoryTeller
StoryTeller is a tool by Jeremy Miller, inspired by FitNesse. In StoryTeller, developers define a grammer for tests and then non-developer users can build specifications using a GUI. Because the GUI assists the user with the grammer StoryTeller is probably easier to use for non-technical people.
Executable specifications are an exciting idea, and I hope that one day it will be the way that we all build software. At this moment the tooling is rapidly improving. I like the text based tools for use by developers with BDD style functional testing, and I like StoryTeller for its improved support for customer involvement.
- Posted by liammclennan on June 23, 2009
UPDATE: I have since discovered that these methods are already implemented as part of the jQuery utilities methods.
In C# and Ruby we can use the Where() and Select() methods to select elements from a collection by testing each element with a predicate (method that returns a boolean). Unfortunately, javascript does not have this capability. Here is the code to add a where() method to the javascript array class:
// predicate is a function that accepts one parameter and returns a boolean.
Array.prototype.where = function(predicate) {
var derivedArray = [];
for (i = 0; i < this.length; i += 1) {
if (predicate(this[i])) {
derivedArray.push(this[i]);
}
}
return derivedArray;
});
The prototype property on the Array class is a reference to the object from which all arrays are created, so whatever we add to Array.prototype becomes part of every array that has or will be created. To use the above method:
var evens = [1,2,3,4,5,6,7,8,9,10].where(function(element) {
return element % 2 === 0;
});
// the output from the above method call will be [2,4,6,8,10]
The predicate passed to the where function can be either an anonymous method or a regular method like so:
function isEven(element) {
return element % 2 === 0;
}
var evens = [1,2,3,4,5,6,7,8,9,10].where(isEven);
// the output from the above method call will be [2,4,6,8,10]
Another useful method is called collect or map in ruby and select in C#. Here is the description from the ruby documentation, "Invokes block (anonymous method) once for each element of self. Creates a new array containing the values returned by the block."
Here is a javascript implementation of this method:
// operation is a function that accepts one parameter and returns a value.
Array.prototype.collect = function(operation) {
var derivedArray = [];
for (i = 0; i < this.length; i += 1) {
derivedArray.push(operation(this[i]));
}
return derivedArray;
});
and the usage is:
var doubled = [1,2,3,4,5,6,7,8,9,10].collect(function(element) {
return element * 2;
});
// the output from the above method call will be [2,4,6,8,10,12,14,16,18,20]
The naming of some of these methods is confusing so here is a table explaining the differences.
| |
Selects a subset of the elements of a collection. |
Transforms the elements of a collection. |
| Ruby |
select |
map or collect |
| C# |
Where |
Select |