Skip to content

Instantly share code, notes, and snippets.

@jjb
jjb / gist:2347804
Created April 10, 2012 01:30
How to build the pg gem on os x with macports

First, install your preferred version of postgres

sudo port install posgresql84

Now you have psql84 available, which rolls right off your fingers. Even better, your pg_config is buried in a postgresql84 namespace. Fix both of these with:

sudo port select --set postgresql postgresql84

Now you can intall the gem

# TODO: write blog posts
@jjb
jjb / 1. code.rb
Created May 13, 2012 05:56
Usage of Timeout#timeout
require 'timeout'
begin
Timeout.timeout(1) do
# spending time doing, say, an IO operation accross the network
sleep 2
end
rescue
puts "Getting the big file took too long. Try again later."
end
@jjb
jjb / gist:2686448
Created May 13, 2012 06:17
Bare ruby rescue handles StandardError and its descendants
class SpecialError < StandardError; end
begin
raise StandardError
rescue
"rescued"
end
# "rescued"
begin
@jjb
jjb / 1. code.rb
Created July 13, 2012 14:37
Problems with Timeout.timeout's use of Thread#raise
require 'timeout'
def process_foos(error_to_rescue)
begin
# -> process Foos
# -> if we run out of Foos, raise
sleep 2
rescue error_to_rescue
# -> email the manager that we ran out of Foos
puts <<-MESSAGE
@jjb
jjb / gist:3149039
Created July 20, 2012 06:26
tests illustrating problems with Timeout.timeout
def subject(throws, catches)
$inner_succeeded=nil
$raised_in_inner=nil
$caught_in_inner=nil
$raised_in_outer=nil
$not_raised_in_outer=nil
begin
Timeout.timeout(0.1, throws){
begin
@jjb
jjb / gist:3237811
Created August 2, 2012 15:13
List an ActiveRecord model's callbacks
self.class._create_callbacks.select { |callback| callback.kind.eql?(:after) }.collect(&:filter)
@jjb
jjb / 1.rb
Created August 25, 2012 21:22
method which accepts multiple blocks
def generate_continue_object(*args)
args.each{|a| puts a.inspect}
yield if block_given?
continue_object = Object.new
def continue_object.next_block
if block_given?
yield
@jjb
jjb / 1.code.rb
Created September 15, 2012 05:38
t = Thread.start{
begin
puts "starting executing code!"
sleep 1
puts "done executing code!" # we don't expect to reach here
rescue Exception
puts "rescuing!" # we don't expect to reach here
ensure
puts "ensuring!" # will we reach here????? (yes)
end
@jjb
jjb / 1. thread_raise.rb
Created September 16, 2012 00:19
Ruby Thread#raise raises at wherever the thread happens to be
require 'thread'
t = Thread.new{
sleep 0.1
sleep 0.1
sleep 0.1
sleep 0.1
sleep 0.1
}