14 Jan 2010

Why I like Enumerable#inject

Recently I’ve been helping a friend learn Ruby. Digging around first year university assignments, I stumbled on a method definition for calculating the annual interest on a bank balance that’s compounded monthly.

The original implementation was in Java, which you could imagine would be more verbose.

def calculate_interest(rate)
  monthly_balance = @balance
  interest = 0
  12.times do
    monthly_interest = monthly_balance * rate
    interest = interest + monthly_interest
    monthly_balance = monthly_balance + monthly_interest
  end
  interest
end

Look at all those variables. Yuck. I would never write code like this today. Accumulation is a perfect candidate for Enumerable#inject.

def calculate_interest(rate)
  balance_plus_interest = 12.times.inject(@balance) do |balance, m| 
    balance += balance * rate
  end
  balance_plus_interest - @balance
end

Storing the result is unnecessary.

def calculate_interest(rate)
  12.times.inject(@balance) { |b, m| b += b * rate } - @balance
end

Although unnecessary, I’d argue it may lead to less readable code.

Lastly, while you’re not prevented from chaining method invocations to the end keyword, it doesn’t feel right. That said, I’m open to change.

def calculate_interest(rate)
  12.times.inject(@balance) do |balance, m| 
    balance += balance * rate
  end - @balance
end

05 Dec 2009

Forking Jekyll: Now with LESS and Growl notifications

Introducing tatey-jekyll, a fork which adds support for compiling LESS into CSS, optional Growl notifications with build statuses and a Liquid permalink tag for linking to published posts.

It’s compatible for sites built with Jekyll 0.5.4.

Install

You’ll need to install the LESS gem and replace the .css extensions on your stylesheet with .less. LESS will compile your stylesheets without additional modification.

$ gem install tatey-jekyll -s http://gemcutter.org

If you’re a Mac OS X user with Growl installed, you’ll want to install growlnotify (Command line tool available in Growl Extras) and the Growl gem which provides Ruby bindings to growlnotify.

Motivations

Jekyll is my favourite piece of blogging software. More recently, I’ve been thinking about using Jekyll to build a couple of business to business orientated sites at work.

We need to get them out quickly, and we’re not expecting them to change frequently. A full blown content management system is overkill, and a pure static site is a maintenance nightmare. Jekyll is the perfect match.

This project in combination with my working experience of Jekyll has lead to me add a couple of features which will continue to make life easier for myself and my colleagues.

LESS

LESS is an extension to CSS which adds variables, nesting and mixins. Unlike Sass, LESS strives for familiarity and backwards compatibility with CSS.

After migrating from LESS, my stylesheets are more organised and reusable. Take a look at the before and after for tatey.com.

Growl notifications

Growl notifications with Jekyll build statuses

Stop killing your browser’s reload button and let Jekyll notify you when the build is complete.

Link to a published post by its name and the permalink tag will be compiled into an anchor. The permalink tag will respect your post’s permalink structure.

{% permalink 'Earlier post', '2009-09-09-foo' %}

…is compiled to

<a href="/2009/09/09/foo/">Earlier post</a>

Notes

If you’re looking for Haml and Sass compatibility, you ought to checkout Henrik Nyh’s fork.


15 Nov 2009

Automating builds for Integrity when using a vanilla Git repository

Integrity is a light-weight continuous integration server.

It supports automatically building your projects when you configure a post-receive hook on GitHub to POST to your installation of Integrity. If you’re not using GitHub, you can still achieve automated builds using a similar mechanism.

A quick look at the Fetch and build button on the project page reveals the URL you need to POST for initiating a build. Create an executable script and include it in the post-receive hook of your vanilla Git repository.

#!/usr/bin/env ruby
require 'net/http' 
Net::HTTP.post_form(
  URI.parse('http://server:9292/project-name/builds'), ''
)

If you’re using Rails you’ll want to run your migrations before running your tests. Create a Rakefile for continuous integration.

desc "Runs migrations and all tests"
task :ci => ['db:migrate', 'test', 'cucumber']

Lastly, update the build script for your project to run the continuous integration task.

rake ci


10 Nov 2009

What I aspire to in minimal web design

The Hypsometry Blog, by Christopher Boone

Dive Into HTML5

Constantly impressed with what I find in the Sites page on the Jekyll wiki (and my inbox).

Updated 11 Nov 2009

The 100% Easy-2-Read Standard is a fantastic foundation for designing a website. Wishing I read this earlier.


02 Nov 2009

Quotes is a Sinatra application for browsing and submitting IRC quotes

Having frequented IRC since 1999, I’ve always been disappointed with existing quote submission applications for their ugliness. Itching to try out Sinatra, Sequel and jQuery I started (re)writing Quotes last coding night.

From the code, to the design and ultimately the user experience; the objective of Quotes was to be simple. I wanted users to receive immediate feedback for any action that’s performed. I want quotes to be so pretty that you’d scroll up and down the page a couple of times.

It has a tiny footprint. There’s only three models, a few actions and a couple of views. Without caching it’s still fast to load on a resource limited environment, such as DreamHost. Sinatra was a perfect match for this tiny project.

Code is on GitHub and you can view a live example of the application. Thanks to #lolbot on irc.freenode.com for the motivation, Ashley Kyd for the concept and Pascal Klein for the initial design.


← Older

Tate Johnson is a 23 year old Ruby on Rails developer and university student living in Brisbane, Australia. He enjoys riding bicycles, motorbikes, taking photos and travelling.