John Bachir

Why it's a good idea to put a newline at the end of every file

I will today settle one of the great issues of our time: whether or not to always put a newline at the end of a file.There are two reasons why it's a good idea, and zero reasons why it's a bad idea.

Let's consider these files:

https://gist.github.com/4010011

The first reason: when using various command . . .

Read More

November 23, 2013

sane_timeout: a replacement for Ruby's standard library Timeout

Ruby's Timeout library had a serious problems before 1.9: it would sometimes not timeout. This was solved by system-timer(see the readme for more background). But in 1.9, we finally have a Timeout thatreliablytimes out, joy. However, it still has some problems.

As some quick background, here is the basic usage:

require . . .

Read More

November 23, 2013

The somewhat peculiar behavior of Ruby's Thread#raise

Here's something that's perhaps not entirely obvious: when you call Thread#raise, the exception will be raised at whatever point of execution that thread happens to be at.

require 'thread'

t = Thread.new{
 sleep 0.1
 sleep 0.1
 sleep 0.1
 sleep 0.1
 sleep 0.1
}

sleep rand(4) * 0.1 . . .

Read More

November 23, 2013

Ruby Thread#kill and ensure blocks

Here's something interesting in Ruby: If a thread is killed and the portion of code that was running has a corresponding ensure block, that block will be executed before the thread is killed. This is nice in that the utility of ensure is maintained (IO objects will be closed, etc.), but it's perhaps unintuitive that the semantics of . . .

Read More

November 23, 2013

PostgreSQL hstore

With its newish hstore extension, Postgres now has the ability to store schemaless data, like MongoDB, Riak, and other newfangled data stores. I've been wanting to play with it for a while and recently started on something that I thought might benefit from it. Inside model A, I'm storing an arbitrary number of rows of model B, which . . .

Read More

November 23, 2013

Passing multiple blocks to a Ruby method

Something I've wanted to toy around with for a while -- here's a little pattern for passing multiple blocks to a Ruby method. This is something I find myself wishing for now and again, although at the moment I can't think of what one of those use cases were.

def generate_continue_object(*args)
 args.each{|a| puts a.inspect . . .

Read More

November 23, 2013

Archive