Relocation notice


The Gemfile blog will continue life at If you hurry, you might be the first one to read my brand-new article Empowering Null Objects in a Rails Application.

See you there!


Selective stubbing of method calls in RSpec

While building a simple API in Rails, I had the following method in my User model that needed to be tested:

def generate_auth_token!
  loop do
    self.auth_token = Devise.friendly_token
    break unless self.class.exists?(auth_token: auth_token)

The method uses Devise’s friendly_token method to generate an authentication token for a new user record. After calling the method once, the loop checks whether a user record with the same auth token already exists. If the answer is positive, the method goes for a second try; otherwise it breaks out of the loop, mission accomplished. Continue reading

Tagged , ,

€ £ $ ¥ ₽

Thanks to Stack Overflow’s annual survey, we now have this gem (pun intended) of data:

Screen Shot 2015-06-11 at 9.38.02 AMScreen Shot 2015-06-11 at 9.39.20 AM

Is this as absurd as it appears? Let’s take a closer look. Continue reading

Tagged , , , , , ,

Git hooks, ctags & Rails

Small adjustment to Tim Pope’s outstanding Effortless Ctags with Git:

To get ctags to also index all bundled gems (including Rails), make the following change to the ctags hook


git ls-files | \
  ctags --tag-relative -L - -f"$dir/$$.tags" --languages=-javascript,sql


ctags --recurse --tag-relative -f"$dir/$$.tags" --languages=-javascript,sql `bundle show --paths` `git ls-files`

After triggering the hook, the tags file takes a few seconds to generate in the background – have patience 🙂


That nondescript title is not a placeholder – this post is really about the underscore. It arose out of one of those rewarding moments where you suddenly feel that there must be a way to do something better, and a search quickly reveals that, indeed, there is.

Ever evaluate a complex – particularly multi-line – expression in irb or Rails console and immediately after hitting the Return key realize that you’ve neglected to assign the return value to a variable? Suffer no more:

2.2.0 :001 > [[1,2], [3,4]].map do |pair|
2.2.0 :002 >   pair[0]*2 + pair[1]*3
2.2.0 :003 > end
=> [8, 18]
2.2.0 :004 > _
=> [8, 18]
2.2.0 :005 > { |c| c*2 }
=> [16, 36]

See that little line on lines 4 & 5?! Saved by the humble underscore.



Calling the [] method on Hash converts a one-dimensional array with an even number of elements into a hash, with odd/even elements alternating as keys/values. Very cool 🙂

$ irb

2.2.0 :001 > Hash['key1', 'value1', 'key2', 5]

 => {"key1"=>"value1", "key2"=>5}

2.2.0 :002 > Hash['key1', 'value1', 'key2']

ArgumentError: odd number of arguments for Hash

Tagged ,

On Cucumber and testing

This article was originally written as part of an ongoing discussion within the team building the OSRA app.

1. Cucumber.

Any time a component is added to an application, its anticipated benefits must be weighed against the costs of its implementation. Cucumber is a costly framework in terms of development time, developer effort, and cognitive strain on both creators and readers of tests due to the following: Continue reading

Tagged , ,

RSpec 3.0.1 & RubyMine

After the recent RSpec update, warnings started showing up in RubyMine:

Deprecation Warnings: Treating `metadata[:execution_result]` as a hash is deprecated. Use the attributes methods to access the data instead. Called from …/teamcity/rspec3_formatter.rb: line#

(So, RSpec is complaining that RubyMine’s formatter is not accessing its data properly?) The warnings were overwhelming to the point of obscuring the results of running the test. Simple fix: Continue reading

Tagged , ,

Sometimes everyone needs a break

I’ve been working through – and enjoying – the EdX/Berkeley CS169 course for the past couple of months. With a bit of a break between the two halves, I thought I’d start a small Rails project of my own. After including ‘rspec-rails’ in my Gemfile, I ran bundle install and then

$ rails g rspec:install
Could not find generator rspec:install

Continue reading

Tagged , ,

Removing Paperclip attachments

Paperclip’s GitHub page includes the necessary steps to delete an attachment – the model’s attribute that refers to the attachment simply gets set to nil. Here is the full implementation of deleting a user’s profile picture in a Rails app. Start with tests (right?!):


require 'spec_helper'

feature 'Editing Users' do
  let(:user) { FactoryGirl.create(:user) }
  let(:admin) { FactoryGirl.create(:admin) }

  scenario 'removing user profile photo' do
    sign_in_as admin # of course
    visit edit_user_path(user)
    click_link 'Remove profile photo'

    expect(page).to have_content 'User profile photo has been removed.'
    expect(page).not_to have_css("img[src*='user.png']") # file name of the user photo defined in the factory
    expect(page).to have_css("img[src*='paperclip_default.png']") # specified in the User model

Continue reading

Tagged , ,