Code Worrier

Teaching myself to RTFM

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.

Passing Symbols, a Kata Lesson

| Comments

In my post about the kata gem, I promised to share something I learned from the calculator kata. Here goes.

The calculator kata asks you to construct a Calculator class the instances of which are initialized with comma-delimited strings of integers. Next you are asked, one by one, to create a series of instance methods—#sum, #prod, #div, #diff—for the class that perform arithmetic operations on the stored integers.

Starting with #sum only, I wrote the class, obviously enough, like this: