Red Green Repeat Adventures of a Spec Driven Junkie

TIL Feedback in Rails Runner

One reason why I like coding with specs so much is that they provide so much feedback. Even a failing spec is informational.

In Rails Runner, there is no spec to invoke the script (of course, all the methods are spec-ed out,) so I had to get feedback another way.

BTW, Rails Runner is a great tool to run periodic jobs on the side, like daily sales updates.

When in doubt: go caveman style!

Anyways, I tried out the good old caveman style feedback: puts, but nothing.

Tried out the handy pry: binding.pry, to at least pop up a REPL there. Nothing as well. :-/

Went old sk00l caveman, p, that’s gotta do something, right? Nope!

STDOUT.flush??

Then I saw: STDOUT.flush, in the Runner docs.

Kinda sounded like what I want. Let’s try it in the script:

1
2
3
4
5
6
7
8
yesterday = Time.zone.yesterday
from = yesterday.beginning_of_day
to = yesterday.end_of_day

puts from, to
STDOUT.flush

DailySalesReportEmail.generate(from, to)

Boom! All the puts statements were printed to the console!

But why?

But why? Hmmmm… this post provided some insight.

Looks like every print statement is being held in a buffer and will write out when it is full, in this case, probably never for such small output.

Long story short

To get console output in a Rails Runner script, use puts with STDOUT.flush after.

Go forth and get feedback!