Skip to content

Instantly share code, notes, and snippets.

View thinkerbot's full-sized avatar

Simon Chiang thinkerbot

View GitHub Profile
@thinkerbot
thinkerbot / normalize.css
Created March 29, 2010 21:04
css to remove default styles
/* From 'Transcending CSS' by Andy Clark */
/* http://www.transcendingcss.com/ */
/* Normalize margin, padding */
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, p, blockquote, th, td {
margin: 0;
padding: 0;
}
/* Normalize font-size for headers */
@thinkerbot
thinkerbot / goodparts.js
Created April 12, 2010 14:48
Snippets from JavaScript: The Good Parts
// Snippets from 'JavaScript: The Good Parts' by David Crockford
if (typeof Object.beget !== 'function') {
Object.beget = function (o) {
ver F = Function () {};
F.prototype = 0;
return new F();
};
}
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
<script>
var Tap = {};
$(document).ready(function(){
function debug(str){ $("#debug").append("<p>"+str+"</p>"); };
ws = new WebSocket("ws://localhost:8080/websocket");
@thinkerbot
thinkerbot / create.sql
Created June 1, 2010 16:54
stock sql + factories
-- Describes everything needed to create the database schema.
create sequence emp_seq;
create sequence dept_seq;
create sequence job_seq;
create table dept (
id number(15) not null,
name varchar2(20) not null,
city varchar2(20) not null,
@thinkerbot
thinkerbot / gsub_vs_benchmark.rb
Created June 13, 2010 01:50
optimization of Rack::Utils.escape_html
require 'test/unit'
require 'benchmark'
# This benchmark compares the speed of several variations of the escape_html
# method in Rack::Utils (4bfb6fc2768312e8899759255904463b781a10cd)
#
# # Escape ampersands, brackets and quotes to their HTML/XML entities.
# def escape_html(string)
# string.to_s.gsub("&", "&amp;").
# gsub("<", "&lt;").
@thinkerbot
thinkerbot / tiobe.txt
Created June 21, 2010 14:33
BASH examples
# cut -f 5 tiobe.txt | xargs ruby -e "puts ARGV.inject(0) {|sum, n| sum + n[0..-1].to_f }"
1 1 Java 18.033% -2.11% A
2 2 C 17.809% +1.03% A
3 3 C++ 10.757% +0.16% A
4 4 PHP 8.934% -0.74% A
5 5 (Visual) Basic 5.868% -2.07% A
6 7 C# 5.196% +0.66% A
7 6 Python 4.266% -0.49% A
8 9 Perl 3.200% -0.71% A
require 'benchmark'
require 'active_record'
ActiveRecord.load_all!
# ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)
@thinkerbot
thinkerbot / patch.rb
Created July 22, 2010 22:56
Study of AR select + include
# Patch fixes issue on oracle.
class ActiveRecord::Base
NONQUOTED_OBJECT_NAME = /[A-Za-z][A-z0-9$#]{0,29}/
NONQUOTED_DATABASE_LINK = /[A-Za-z][A-z0-9$#\.@]{0,127}/
TABLES_IN_STRING = /((?:#{NONQUOTED_OBJECT_NAME}\.)?#{NONQUOTED_OBJECT_NAME}(?:@#{NONQUOTED_DATABASE_LINK})?)\..?/
def self.tables_in_string(string)
return [] if string.blank?
# string.scan(/([\.a-zA-Z_]+).?\./).flatten
string.scan(TABLES_IN_STRING).flatten.map {|str| str.downcase }.uniq - ['raw_sql_']
@thinkerbot
thinkerbot / tables_in_string_test.rb
Created July 26, 2010 16:45
tables_in_string patch
require 'test/unit'
# These tests illustrate that the tables_in_string will not properly handle
# oracle table names containing valid non-word characters like '$' or those
# suffixed with a db link. As a consequence, eager loading can revert to the
# left-outer-join strategy and ignore select statements on find (see
# http://gist.github.com/486750).
#
# To fix the issue, the regexp in tables_in_string needs to be updated to
# handle fully-qualified oracle names. As you can see it's kinda ugly to do
# :startdoc::generator
class Doctest < Tap::Generator::Base
Utils = Lazydoc::Utils
Comment = Lazydoc::Comment
def manifest(m, *paths)
paths.each do |path|
lines = File.read(path).split("\n")