Skip to content

Instantly share code, notes, and snippets.

View thinkerbot's full-sized avatar

Simon Chiang thinkerbot

View GitHub Profile
@thinkerbot
thinkerbot / hash_bug.rb
Created August 20, 2009 05:22
Array.hash error for Bignum
require 'rubygems'
# Array.hash for objects do not produce a Fixnum hash will blow up. This issue
# will be fixed in a future release but I thought you might like to know in
# case you wanted to patch RubyGems for versions prior to the fix.
#
# (see http://redmine.ruby-lang.org/issues/show/1883)
#
# I found this error because it prevents an array of Gem::Specifications from
# being converted to YAML, as shown below. Note that in the example rack was
@thinkerbot
thinkerbot / readline_example.rb
Created August 20, 2009 22:34
A Readline example
require 'readline'
Signal.trap('INT', 'SIG_IGN') { puts "hello" }
while line = Readline.readline('--/', true)
exit if line == "exit"
puts line
end
@thinkerbot
thinkerbot / cache_benchmark.rb
Created September 6, 2009 20:12
A DSL pattern supporting inheritance
require 'benchmark'
# A basic version of a dsl that registers key-value pairs onto a Class. When
# looking up a key-value pair, Classes that use the dsl will check the
# registry of each ancestor in order, as if looking up a method.
#
# Call cache_ancestors! to cache the ancestry and speedup lookup.
module Dsl
def self.extended(base)
base.registry ||= {}
@thinkerbot
thinkerbot / app.rb
Created September 18, 2009 19:22
A basic RESTful Sinatra application
require 'rubygems'
require 'erb'
require 'sinatra/base'
# A basic RESTful Sinatra app for serving some kind of object. Uses views:
#
# views
# |- error.erb
# |- index.erb
# |- layout.erb
@thinkerbot
thinkerbot / formatter.rb
Created September 22, 2009 16:00
Custom RedCloth formatting
require 'rubygems'
require 'redcloth'
module CustomHTML
include RedCloth::Formatters::HTML
def custom(opts)
"<em>#{opts[:text].sub("hard.", "easy!")}</em>"
end
end
@thinkerbot
thinkerbot / xslt.js
Created September 23, 2009 16:19
XSLT formatter in javascript
// Adapted from: http://www.w3schools.com/xsl/xsl_client.asp
Xslt: {
// Transforms xml using the specified xsl and returns a document fragment.
// The fragment may be added to the DOM using jQuery like this:
//
// node = Xslt.transform("/example.xml", "/example.xsl");
// jQuery("#node_id").replaceWith(node);
//
transform: function(xml_url, xsl_url) {
@thinkerbot
thinkerbot / prompt_test.rb
Created September 26, 2009 22:04
Failed models for a prompt test
def prompt_test(cmd, script, prompt)
cmd, leader = cmd.lstrip.split(/^/, 2)
inputs = []
expected = [leader]
script.lstrip.split(/^#{prompt}/).each do |lines|
next if lines.empty?
input, output = lines.split(/^/, 2)
inputs << input
@thinkerbot
thinkerbot / Gemfile
Created September 29, 2009 19:56
CC Gemfile and Rakefile Prototypes
spec = eval(File.read('project.gemspec'))
gem "#{spec.name}", "#{spec.version}"
bin_path "vendor/gems/bin"
if ENV['BUNDLE_CC'] == "true"
# in the CC environment, bundles against the
# most recent working copies of a gemspec
clear_sources
cc_dir = ENV['CRUISE_DATA_ROOT'] || File.dirname(__FILE__) + "/../../.."
@thinkerbot
thinkerbot / simple_form.rb
Created October 2, 2009 20:25
Simple Forms in Textile
require 'redcloth'
module SimpleForm
include RedCloth::Formatters::HTML
def form(opts)
content = opts[:text].split("<br />")
definition = content.shift
definition =~ /\A\s*(get|post)(.*?)(?:\[(.*)\])?\z/
@thinkerbot
thinkerbot / curb_session.rb
Created November 5, 2009 20:57
A session wrapper for curb
require 'curb'
class Session
# The internal Curl::Easy instance
attr_reader :curb
# The last response, or nil
attr_reader :last