Code Worrier

Teaching myself to RTFM

Put Your Git Branch in Your Bash Prompt

| Comments

Lots of folks were interested in getting git autocomplete going in bash, so I thought I’d offer another useful git/bash tip. This one was suggested by Stephan in the comments of that earlier post.

If you’re using git properly and collaborating on a project, chances are you find yourself changing branches often. This can be quite disorienting. You deploy from the master branch, then get back to that bug fix you were about to work except for—whoops!—now you commited that fix to master and have to back away slowly. Confusions like this one can easily be avoided by putting you branch name front-and-center: at the end of your bash prompt. Here’s how.

How Do You Prevent ActionMailer From Spamming People in Development?

| Comments

I’ve been working on a Rails app that contains lots of mailers with hard-coded recipients. For instance, here’s a mailer that we use to notify folks when someone asks an urgent question on our hotline:

hotline_mailer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
class HotlineMailer < ActionMailer::Base

  default :from => "notifications@mywebapp.com"

  def notify_urgent_question(question)
    @question = question
    send( :to => "urgent@mywebapp.com",
          :subject => "Urgent: #{question.title}")
  end

  # Other mailer methods...

end

The email address “urgent@mywebapp.com” generates emails to many people. We handle membership in Google apps.

Left to its own devices, this mailer will trigger an email to that list of people every time you hit the controller action that calls for it to be delivered.

That’s bad news if you’re just testing out the action in your development environment. You have a group of people who are getting annoying emails for no reason, and you may not even be getting the emails yourself!

A Tiny Benchmarking Utility

| Comments

While working on a longer (forthcoming) post, I wrote a little benchmarking utility that I want to share. Here it is:

tiny_timer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module TinyTimer
  def self.benchmark(samples=10)

    times = []

    samples.times do
      start = Time.now
      yield
      times << (Time.now - start)
    end

    times.sum / samples.to_f

  end
end

Invoke a Rake Task From Another Task

| Comments

You can invoke a rake task from another rake task. Here’s how:

examples.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace :examples do

  desc "Inner task."
  task :inner_task => :environment do
    # Some code that accomplishes a task.
  end

  desc "Outer task."
  task :outer_task do
    # Some code.
    Rake::Task['examples:inner_task'].execute #Execute innter task.
    # Some other code.
  end

end

Quick Tip: More Legible Output With ‘Kernel#y’

| Comments

Ever tried to get a sense of an ActiveRecord object by squinting though output like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
>> User.find_by_login("Michael Hoffman").recipes.last
=> #<Recipe id: 18891, title: "Pasta with Lemon-Parmesan Butte
r", created_at: "2012-08-29 00:03:55", updated_at: "2012-08-29
16:20:58", description: "I was walking by a little pizzeria in
the East Vill...", recipe_category_id: 36, serving_size: "4 as
a pasta course, 2 as a main", user_id: 784, editors_pick: fals
e, editors_comments: "", label: "pasta_with_lemonparmesan_butt
er", flag_inappropriate: false, wine_id: nil, recipe_style: "a
b", varietal_id: nil, rating_average: 0, makes_serves: "Serves
", flag_inappropriate_comments: nil, sommelier_comments: nil, 
comments_count: 1, is_winner: false, interest: 1, is_finalist:
nil, new_categories_added: nil, admin_categorization_note: nil
>

Yuck.

Why RTFM Is Some Bullshit

| Comments

Learning to code is hard.

I’m not talking about the sense in which we’re all still learning. I mean when you first start out, the first few months. During this period, every tiny bit of progress you make comes at the expense of an excruciating, time-consuming trial.

Personally, I had the great fortune of being around patient, empathic experts who helped me through the worst of it. (Thanks Jon, Jackie, Lauren, et al!) But not everyone is so lucky, and sooner or later a new programmer is bound to encounter RTFM and its new-fangled cousins, GIYF and LMGTFY.

All Your Undefined Vars Are Evaluate to Nil.

| Comments

(NB: I learned much of what I discuss here from this post.)

What does a brand new, never-before-mentioned variable evaluate to in Ruby? Perhaps it seems that the correct answer is: it depends what kind of variable. Undefined instance variables, for example, evaluate to nil, whereas undefined local variables can’t be evaluated at all.