Monthly Archives: March 2014


This Ruby method returns nil rather than raising an exception when called on a non-existent object.

$ rails console

2.1.0 :001 > nil.admin?
NoMethodError: undefined method 'admin?' for nil:NilClass
... stack trace ...

2.1.0 :002 > nil.try(:admin)
 => nil

Continue reading

Tagged ,

Avoid RSpec and Capybara naming conflicts

There I was, testing user authorization within the Posts controller of my blogging app, when I hit a wall:

describe 'authorization' do
  describe 'in the Posts controller' do
    describe 'attempting to issue a direct POST request without being signed in' do
      before { post posts_path }
      specify { expect(response).to redirect_to signin_path }

Simple. But it fails. It works just fine a few dozen lines above within the “Users controller” test block, but fails here. What gives?

Continue reading

Tagged , ,

Just a little proud of my Ruby

Topic 1:

<%= link_to pluralize(topic.posts.count, 'article'), topic_path(topic) %>

on this topic written by

<%= sanitize( { |p| link_to(, user_path(p.user)) }.uniq.join ', ') %>

Nuking all changes with Git

After looking it up twice in one (less than productive) day…

$ git reset --hard # delete all changes from tracked files

$ git clean -f -d # remove untracked files and directories

(Warning: This is destructive! Code will be irreversibly lost.)

Continue reading

Tagged , , , ,

Enjoy your day-of-the-week :)


$ rails console

2.1.0 :001 >
  => 4   #Thursday

2.1.0 :002 > Date::DAYNAMES
  => ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

2.1.0 :003 > "Enjoy your #{Date::DAYNAMES[]}."
  => "Enjoy your Thursday."
Tagged ,

Rails blog comments with AJAX

Basic implementation of blog post comments posed little challenge and differed little from creation of Users and Posts resources. The only wrinkle came from my first use of nested resources, with Comments being a nested resource of Posts. It took a bit to get used to named routes such as


in place of the familiar


or modifications to Rails helpers like

<%= form_for [@post, @new_comment] do |f| %>

So far, it seems that while nesting resources makes perfect sense theoretically and logically, the increased awkwardness that comes with their practical use is not justified. Perhaps this is simply a matter of becoming more familiar with the altered syntax. Continue reading

Tagged , , ,