Skip to content

Instantly share code, notes, and snippets.

/*
* Preload images that aren't visible on page load
* Works for images and background images
*
* $("#img1, #div2").preload()
*/
$.fn.preload = function() {
this.each( function() {
src = $(this).attr("src");
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "is not an email") unless
value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
end
end
@ankane
ankane / f1_score.rb
Created December 11, 2011 17:41
Calculates F1 score
# http://en.wikipedia.org/wiki/F1_score
# true positives, false positives, false negatives
def f1_score(tp, fp, fn)
precision = tp / (tp + fp).to_f
recall = tp / (tp + fn).to_f
2.0 * precision * recall / (precision + recall)
end
cd /tmp
wget http://apache.mirrors.pair.com/httpd/httpd-2.4.2.tar.bz2
tar xzvf httpd-2.4.2.tar.bz2
cd httpd-2.4.2
./configure
make
sudo cp support/ab /usr/sbin
@ankane
ankane / clone_repos.rb
Created January 31, 2013 06:53
Clone all Heroku repos
output = %x[heroku apps]
repos = output.split("\n").select{|line| line.length > 0 and line[0..2] != "===" }.map{|line| line.split[0] }
repos.each do |repo|
unless Dir.exists?(repo)
puts %x[git clone [email protected]:#{repo}.git]
end
end
#!/bin/bash
# From http://chrismccord.com/blog/2013/01/09/better-heroku-db-push-and-db-pull/
#
# Postgres equivalent to heroku db:push.
# Pushes local database up to heroku application database.
#
# Requirements: psql --version >= 9.2.2
#
# Usage:
@ankane
ankane / README.md
Last active December 14, 2015 03:39 — forked from panthomakos/benchmark.rb

Benchmark Bundler

Because loading gems can take longer than you think

Now available as a gem - get it here

@ankane
ankane / map.html.slim
Created March 29, 2013 07:09
Super-simple Google Maps
= javascript_include_tag "http://maps.googleapis.com/maps/api/js?sensor=false"
#map style="height: 500px;"
javascript:
var pings = #{@pings.to_json(root: false).html_safe};
$(document).ready( function() {
var mapOptions = {
zoom: 13,
-- median
-- http://www.postgresonline.com/journal/archives/67-Build-Median-Aggregate-Function-in-SQL.html
CREATE OR REPLACE FUNCTION array_median(numeric[])
RETURNS numeric AS
$$
SELECT CASE
WHEN array_upper($1,1) = 0 THEN null
WHEN mod(array_upper($1,1),2) = 1 THEN asorted[ceiling(array_upper(asorted,1)/2.0)]
ELSE ((asorted[ceiling(array_upper(asorted,1)/2.0)] + asorted[ceiling(array_upper(asorted,1)/2.0)+1])/2.0) END