Skip to content

Instantly share code, notes, and snippets.

@techbelly
techbelly / gist:3284608
Created August 7, 2012 11:20
Old Bailey Scraper
require 'hpricot'
require 'open-uri'
class OldBaileyParser
def initialize
scrape_reports
parse_reports
end
@techbelly
techbelly / gist:3024543
Created June 30, 2012 16:31
Very rough number of methods in rails.
$ git clone https://github.com/rails/rails.git
Without tests
$ find rails -name \*.rb | grep -v test | xargs grep '^[[:space:]]\+def' | wc -l
5587
Wit tests
@techbelly
techbelly / screenshot-diff.rb
Created June 26, 2012 14:30
Quick tool for CSS Refactoring - diffs screenshots against a reference
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
require 'chunky_png'
require 'tempfile'
Capybara.run_server = false
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
def encode(arr)
last_byte = nil
count = 1
(arr + [nil]).each do |byte|
if byte == last_byte && count < 255
count = count + 1
else
yield [count,last_byte] if last_byte
last_byte = byte
count = 1
@techbelly
techbelly / gist:1493576
Created December 18, 2011 14:28
Biased shuffle
def swap(array,indexa,indexb)
value_to_swap = array[indexa]
array[indexa] = array[indexb]
array[indexb] = value_to_swap
end
def biased_shuffle(array)
array = array.dup
array.size.times do |i|
swap(array,rand(array.size),i)
- (NSString *)stringByCapitalisingFirstLetter:(NSString *)string
{
return [string stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[string substringToIndex:1] uppercaseString]];
}
@techbelly
techbelly / gist:1261289
Created October 4, 2011 10:07
EC2 facter
require 'open-uri'
require 'timeout'
require 'ping'
def can_connect?
begin
Timeout::timeout(2) do
open("http://169.254.169.254")
return true
end
require 'test_helper'
require 'capybara/rails'
Capybara.default_driver = :selenium
class AddingPartsToGuidesTest < ActionDispatch::IntegrationTest
include Capybara::DSL
def teardown
DatabaseCleaner.clean
require 'test_helper'
require 'capybara/rails'
Capybara.default_driver = :selenium
class AddingPartsToGuidesTest < ActionDispatch::IntegrationTest
include Capybara::DSL
def teardown
DatabaseCleaner.clean
for i in `find . -name .git | xargs -n1 dirname`
do
pushd $i
git status -uno -s
popd
done