Skip to content

Instantly share code, notes, and snippets.

@henrik
henrik / rails_ext.rb
Created December 5, 2008 11:32
Make Rails' parameterize remove apostrophes.
module ActiveSupport::Inflector
# Parameterize "Foo's and Charles's bar" into "foos-and-charles-bar"
# instead of into "foo-s-and-charles-s-bar".
def parameterize_with_apostrophe_removal(string, sep = '-')
string = string.dup
string.gsub!(%r{s's\b}i, 's')
string.gsub!(%{'}, "")
parameterize_without_apostrophe_removal(string, sep)
end
@torsten
torsten / proxy.rb
Last active April 30, 2024 17:53
A quick HTTP proxy server in Ruby.
#!/usr/bin/env ruby
# A quick and dirty implementation of an HTTP proxy server in Ruby
# because I did not want to install anything.
#
# Copyright (C) 2009-2014 Torsten Becker <torsten.becker@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
@sunlightlabs
sunlightlabs / levenshtein.rb
Created August 19, 2009 18:07
Ruby C implementation of Levenshtein
# C implementation of Levenshtein text distance algorithm, uses RubyInline to call from within Ruby
# Wildly faster than the Text gem's Text::Levenshtein
# Example:
# l = Levenshtein.new
# l.distance 'hello', ' hello'
# => 1
# Taken from http://www.benallfree.com/2008/10/05/finding-duplicate-text-with-ruby/
require 'builder'
class Sitemap
STATIC_URLS = ['/home/about',
'/home/help',
'/home/contact_us',
'/home/terms']
class << self
@muddana
muddana / sentiment_classifier.py
Created March 14, 2010 00:38
sentiment analysis of twitter data.
# down the test/train data : http://www.stanford.edu/~alecmgo/cs224n/twitterdata.2009.05.25.c.zip
#into a folder "corpus" at the level of this file (else u need to change inside the code for now)
import nltk
import random
import re
import gc
from pymongo import Connection
#method defs
@tobitailor
tobitailor / get_barcode_from_image.js
Created June 1, 2010 19:33
Barcode recognition with JavaScript - Demo: http://bit.ly/djvUoy
/*
* Copyright (c) 2010 Tobias Schneider
* This script is freely distributable under the terms of the MIT license.
*/
(function(){
var UPC_SET = {
"3211": '0',
"2221": '1',
"2122": '2',
@mnutt
mnutt / Instrument Anything in Rails 3.md
Created September 6, 2010 06:50
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

@jharjono
jharjono / sinatra_sass_coffee.rb
Created March 7, 2011 19:12
a setup of Sinatra using Slim for HTML, Sass for CSS, and CoffeeScript for JavaScript
#!/usr/bin/env ruby
# Libraries:::::::::::::::::::::::::::::::::::::::::::::::::::::::
require 'rubygems'
require 'sinatra/base'
require 'slim'
require 'sass'
require 'coffee-script'
# Application:::::::::::::::::::::::::::::::::::::::::::::::::::
@alexbowe
alexbowe / nltk-intro.py
Created March 21, 2011 12:59
Demonstration of extracting key phrases with NLTK in Python
import nltk
text = """The Buddha, the Godhead, resides quite as comfortably in the circuits of a digital
computer or the gears of a cycle transmission as he does at the top of a mountain
or in the petals of a flower. To think otherwise is to demean the Buddha...which is
to demean oneself."""
# Used when tokenizing words
sentence_re = r'''(?x) # set flag to allow verbose regexps
([A-Z])(\.[A-Z])+\.? # abbreviations, e.g. U.S.A.
@hgmnz
hgmnz / query_planner.markdown
Created March 23, 2011 14:14
PostgreSQL query plan and SQL performance notes

Types of index scans

Indexes

Sequential Scan:

  • Read every row in the table
  • No reading of index. Reading from indexes is also expensive.