Skip to content

Instantly share code, notes, and snippets.

require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://learnyousomeerlang.com/content'))
urls = [ 'http://learnyousomeerlang.com/content' ] + doc.xpath('//h3/a').map { |n| n.attribute('href').to_s }
exec("wkhtmltopdf -s A4 #{ urls.join(' ') } learnyousomeerlang.pdf")
$ rake metrics:all
(in /home/matt/brute_force_capatcha)
saikuro --filter_cyclo 0 --warn_cyclo 5 --error_cyclo 7 --output_directory tmp/metric_fu/scratch/saikuro --formater text --input_directory "lib" --cyclo
sh: saikuro: not found
Saikuro failed with exit status: 127
@mattdenner
mattdenner / gist:340143
Created March 22, 2010 14:47
Ruby quine
_="_=%p;puts _%%_";puts _%_
#!/usr/bin/env ruby
require 'rubygems'
require 'amqp'
require 'mq'
require 'uuid'
exchange_type = ARGV.first or raise "Usage: publisher <exchange type>"
[ :direct, :fanout, :topic ].include?(exchange_type.to_sym) or raise "You can only use direct, fanout or topic"
KEYS = [ 'even', 'odd' ]
@mattdenner
mattdenner / publisher.rb
Created March 27, 2010 10:54
RabbitMQ publisher & subscriber in Ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'amqp'
require 'mq'
require 'uuid'
exchange_type = ARGV.first or raise "Usage: publisher <exchange type>"
[ :direct, :fanout, :topic ].include?(exchange_type.to_sym) or raise "You can only use direct, fanout or topic"
KEYS = [ 'even', 'odd' ]
@mattdenner
mattdenner / gist:356359
Created April 5, 2010 13:57
Immutable after construction pattern
class ConstructorHelper
class << self
def construct(target, writers_for = target.instance_variables, &block)
self.new(target, writers_for).instance_eval(&block) if block_given?
end
end
def initialize(target, writers_for = target.instance_variables)
writers_for.each do |variable|
self.class.send(:define_method, :"#{ variable.to_s.sub(/^:?@/, '') }=") do |v|
class MyBase
def do_something
end
end
class MyDerived < MyBase
def do_something
do_something_else
super
end
@mattdenner
mattdenner / card.css
Created August 11, 2010 10:10
How to do backface hiding when -webkit-backface-visibility doesn't work!
/*
The back of the card is rotated 180 degrees from the card itself, giving
the illusion that it really is the back! Unfortunately hiding the back
face of both "faces" doesn't work with webkit builds (requires Snow Leopard!)
but we set it anyway.
*/
.card #back { -webkit-transform: rotateY(180deg); }
.card .face { -webkit-backface-visibility: hidden; }
/*
@mattdenner
mattdenner / vows.rake
Created August 15, 2010 08:59
Snippets for testing Javascript with nodejs and vows (see http://mattdenner.github.com/2010/08/15/testing-javascript.html)
var vows = require('vows'), assert = require('assert'), path = require('path');
var application = require(path.join(process.cwd(), 'public', 'javascripts', 'application'));
@mattdenner
mattdenner / gist:881227
Created March 22, 2011 13:49
Be careful when putting :group on an association used as in a has_many :through, but be aware that it can sometimes give a considerable performance improvement!

I've been trying to track down a performance problem (one of many) within our application that sucks 200 seconds (or more) when doing something really simple. The design of the model is this:

class Study
  has_many :requests
  has_many :projects, :through => :requests, :uniq => true
end

Essentially we have a study that can request some work to be done in our laboratories and that work is done as part of a project, which is responsible for paying for it later. So a study can be related to many projects through the work it has requested.

This all works fine until you want to do eager loading of the projects association, specifically within a query for multiple studies. For example, the killer query comes from: