Skip to content

Instantly share code, notes, and snippets.

@fnando
fnando / html5.js
Created December 1, 2010 22:32
Instantiate HTML5 tags, so we can style them on IE
(function(){
var tags = "abbr|article|aside|audio|canvas|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video".split("|");
for (var i = 0, tag; tag = tags[i]; i++) { document.createElement(tag); }
})();
class Question
CHARS = ("a".."z").to_a
attr_accessor :description
attr_writer :right_answer, :chosen_answer
[:right_answer, :chosen_answer].each do |name|
class_eval <<-RUBY
def #{name}(*args)
@#{name} = args.first if args.any?
@fnando
fnando / polite.rb
Created January 28, 2011 18:12
ActiveRecord macro example
require "active_record"
module Polite
BAD_WORDS = ["fuck", "asshole", "motherfucker", "cunt", "cock", "dickhead"]
ESCAPED = BAD_WORDS.collect {|word| Regexp.escape(word)}
RE = /(#{ESCAPED.join("|")})/i
def self.extended(base)
base.extend ClassMethods
end
@fnando
fnando / app_config.rb
Created January 28, 2011 19:38
Using yield on DSLs
module App
extend self
def configure(&block)
yield config
end
def config
Config
end
@fnando
fnando / message.rb
Created January 28, 2011 20:52
Fluent interface example
class Message
def to(name)
@to = name
self
end
def from(name)
@from = name
self
end
@fnando
fnando / message.rb
Created January 28, 2011 21:26
Fluent interfaces + blocks example
class Message
def initialize(&block)
instance_eval(&block) if block_given?
end
def to(name)
@to = name
self
end
def hello; end
@fnando
fnando / keywurl_import.rb
Created February 24, 2011 23:31
Import Keywurl rules to Google Chrome
require "plist"
require "sqlite3"
APP_SUPPORT = File.expand_path("~/Library/Application Support")
PLIST_FILE = APP_SUPPORT + "/Keywurl/Keywords.plist"
CHROME_FILE = APP_SUPPORT + "/Google/Chrome/Default/Web Data"
plist = Plist.parse_xml(PLIST_FILE)
db = SQLite3::Database.new(CHROME_FILE)
@fnando
fnando / jquery.headify.js
Created February 28, 2011 23:27
Allow table headers to be fixed on the top, with this beautiful and lovely hack.
// Allow table headers to be fixed on the top,
// with this beautiful and lovely hack.
//
// Usage: $("table").headify();
//
// This CSS will get you started:
//
// .headified { width: 230px; }
// .headify-headers { list-style: none; margin: 0; overflow: hidden; padding: 0; }
// .headify-headers li { float: left; }
@fnando
fnando / gist:895099
Created March 30, 2011 19:20
Home Gemfile
global_gemfile = File.expand_path("~/.Gemfile")
eval File.read(global_gemfile), binding if File.exist?(global_gemfile)