Category Archives: ruby

Rake Task To Load Production Data

When I’m working on a Rails app that relies on live data, like a reporting app for example, I find myself constantly wanting to pull live data to my local dev database. So I made a rake task to do this:

namespace :db do
  namespace :heroku do
    desc "Load the PROD database from Heroku to the local dev database"
    task :load => :environment do
      if Rails.env == 'development'
        Bundler.with_clean_env do
          config = Rails.configuration.database_configuration[Rails.env]
          system <<-CMD
            heroku pgbackups:capture
            curl -o latest.dump `heroku pgbackups:url`
            pg_restore --verbose --clean --no-acl --no-owner -h localhost \
              -U #{config["username"]} -d #{config["database"]} latest.dump
            rm -rf latest.dump


Filed under programming, ruby

Ruby Minitest

I had the privilege of attending DCamp this past weekend. It was awesome. While doing the Game Of Life pairing sessions, one of my pair partners wanted to explore the exercise using Minitest. Neither of us had ever used it, so we moved on with RSpec and I made a note to come back to it.

I try to be discerning about bringing additional dependencies (including gems) into a project. I’ve always just used test/unit, and I prefer that syntax over the RSpec syntax. Minitest is baked into the stdlib (starting with Ruby 1.9.3), so you’ve already got everything you need. You can also write tests using either the test/unit or spec style. Win-win right? If you want more there are also additional gems that can supplement functionality. Everybody wins!!!

Here is a basic class, taken from the Minitest gem documentation.


class Meme
  def i_can_has_cheezburger?

  def will_it_blend?

Unit tests, using Minitest:


require "minitest/autorun"
require_relative "../lib/meme"

class TestMeme < Minitest::Unit::TestCase
  def setup
    @meme =

  def test_that_kitty_can_eat
    assert_equal "OHAI!", @meme.i_can_has_cheezburger?

  def test_that_it_will_not_blend
    refute_match /^no/i, @meme.will_it_blend?

  def test_that_will_be_skipped
    skip "test this later"

And a spec testing the same things:


require 'minitest/autorun'
require_relative "../lib/meme"

describe Meme do
  before do
    @meme =

  describe "when asked about cheeseburgers" do
    it "must respond positively" do
      @meme.i_can_has_cheezburger?.must_equal "OHAI!"

  describe "when asked about blending possibilities" do
    it "won't say no" do
      @meme.will_it_blend?.wont_match /^no/i

Outstanding. You can run either of these from the command line:

$ ruby spec/meme_spec.rb
Run options: –seed 25656

# Running tests:


Finished tests in 0.001430s, 1398.6014 tests/s, 2097.9021 assertions/s.

2 tests, 3 assertions, 0 failures, 0 errors, 0 skips

You can also use rake to run the tests, and this is the preferred method.


require 'rake/testtask' do |t|
  t.warning    = false    # this is the default -- shown as example of options
  t.verbose    = false
  t.test_files = FileList['spec/*_spec.rb']
end do |t|
  t.test_files = FileList['test/test_*.rb']

desc "Run ALL tests"
task :default do

Now you can ask rake what tasks it has for you:

$ rake -T
rake default # Run ALL tests
rake spec # Run tests for spec
rake unit # Run tests for unit

Cool. How about we just run the default rake task (ALLTHETESTS):

$ rake
Run options: –seed 28918

# Running tests:


Finished tests in 0.001187s, 1684.9200 tests/s, 2527.3799 assertions/s.

2 tests, 3 assertions, 0 failures, 0 errors, 0 skips
Run options: –seed 26846

# Running tests:


Finished tests in 0.000936s, 3205.1282 tests/s, 3205.1282 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 1 skips

Awesome. You can also run all the tests, just a group of tests, or even just a single test file. This looks tasty enough to overtake test/unit as my go-to testing framework.

Leave a Comment

Filed under programming, ruby

iOS Map Comparison

We experiment with a lot of things at work.  Tools.  Technologies.  Workflows.  Recently we were discussing an effective way to do native iOS development with the team.  I love RubyMotion.  I think it makes it faster and easier to create a native iOS app.  There is a learning curve, though.

We are considering doing a project where the app is native, and written is XCode, but the guts of the app are just web views fed by a Rails app.  But before we could really consider that as an option, we needed to know how it would handle mapping.

So I whipped up a quick and dirty RubyMotion app to see.  I used Leaflet for the HTML version, and had the webpage be served from as a resource in the app.

« 2 of 3 »

I didn’t see anything that scared me off.  You lose the native feel, but I’m not crazy about the MMMapView and it’s annotations.  I’d much rather work with Leaflet and use markers.

So it looks promising.

Here’s a link to the project:

Leave a Comment

Filed under iOS, programming, ruby

Ruby Attr Antipattern

Recently I’ve seen a good handful of something that I’ve never seen before in the Ruby world, and it mystifies me. I’ve been going through exercises on Exercism (, and in the Ruby exercises I’ve seen several people using private attr_accessor. What?

The argument, so far, has been that it’s the same as doing it the long way so why not?

It’s weird. That’s why not. It’s not expressive. That’s why not. It’s a lazy shortcut that still didn’t buy you anything. That’s why not.

An example:

class Demo
  def initialize(something)
    @something = something

  def based_on_something
    something * 2

  attr_reader :something

So, I am all for the use of attr accessors, and I am all for tucking implementation details away in private methods. The use of a private attr does nothing for the latter. You’ve simply added more obscure code that doesn’t tell future you or future maintainers anything about what you intended or thought might happen. If you think the implementation might change or evolve, then go ahead and write the extra 2 lines to define the method explicitly. Otherwise just use the ivar.

Now, it’s entirely possible that I’m just being grumpy old man and shaking my cane. If there is a good reason that’s come along that I haven’t picked up on yet I would love to understand it. For now, though, I’m maintaining that this is a weird antipattern.


Filed under programming, ruby

IRB Benchmark Helper Method

Quick and dirty ruby benchmarking helper that you can stick in your .irbrc:

require 'benchmark'
def benchmark(n, &block) do |x| do
      n.times { }

Then call it like so:

str = "2013-01-21"
benchmark(10) do
benchmark(10) { Date.strptime(str, "%Y-%m-%d") }


Leave a Comment

Filed under programming, ruby