Skip to content

Instantly share code, notes, and snippets.

@jamis
jamis / cap-transcriber.rb
Created July 25, 2011 14:57
Capistrano Transcriber: a wrapper script for Capistrano that writes session transcripts to a timestamped file.
#!/usr/bin/env ruby
require 'capistrano/cli'
class TranscriptLogger
module TranscriptLoggerMetadata
attr_accessor :tx_logger_metadata
def self.apply_to(object, options={})
object.extend(self) unless object.respond_to?(:tx_logger_metadata)
@jamis
jamis / demo.rb
Created August 3, 2011 21:56
Repairing a unicode string that contains invalid characters
# encoding: utf-8
s = "Blah \xe9 blah 헌글"
puts "BEFORE"
puts "encoding: #{s.encoding}"
puts "valid : #{s.valid_encoding?}"
puts "text : #{s}"
s = s.
@jamis
jamis / mysql2-benchmarks.rb
Created August 9, 2011 03:06
mysql vs. mysql2 benchmarks
require 'optparse'
module WordPicker
def pick
self[rand(length)]
end
LENGTHS = [1] + [2]*3 + [3]*4 + [4]*4 + [5]*4 + [6]*3 + [7]*2 + [8]
def sentence
words = []
@jamis
jamis / benchmark-dup.rb
Created September 26, 2015 20:28
Compare Array#dup on 1D array, versus Marshal.load(Marshal.dump(...)) on 2D array
require 'benchmark'
N = 100_000
SIZE = 10
array_1d = Array.new(SIZE * SIZE, 0)
array_2d = Array.new(SIZE) { Array.new(SIZE, 0) }
Benchmark.bm do |bm|
bm.report("1d.dup") { N.times { array_1d.dup } }
@jamis
jamis / string_byteSize.js
Created October 19, 2011 16:22
Compute how many bytes you'd need to represent a Javascript string in UTF-8. This is great if you're doing naughty things like forcing utf-8 encoded text into latin1 database columns, and want to avoid silently truncating text.
/* Returns the number of bytes needed to represent the given
* Javascript string in UTF-8. */
String.prototype.byteSize = function () {
var bytes = 0;
for(i = 0; i < this.length; i++) {
var charCode = this.charCodeAt(i);
if (charCode <= 0x7F)
bytes += 1;
else if (charCode <= 0x7FF)
bytes += 2;
@jamis
jamis / assets_resource.rb
Created October 22, 2011 19:22
Sprockets asset resource for Webmachine (asset pipelining!)
class Resources::Assets < Webmachine::Resource
def content_types_provided
@asset = Application.assets[request.uri.path]
if @asset.present?
[[@asset.content_type, :to_asset]]
else
accept = request.headers['Accept'] || "text/html"
[[accept.split(/,/).first.split(/;/).first.strip, :to_html]]
end
@jamis
jamis / mater_matcher.rb
Created October 20, 2015 16:35
Mater does Rspec: `expect.to not_to`
RSpec::Matchers.define :not_to do
match { |actual| !actual }
end
RSpec.describe "Mater" do
specify "says `to not to' to mean false" do
expect(false).to not_to
expect(true).not_to not_to
end
end
@jamis
jamis / upsilon.rb
Created November 28, 2015 20:45
Implementation of an upsilon grid (tiled octagons & squares) and corresponding maze.
require 'chunky_png'
class Cell
attr_reader :row, :col
def initialize(row, col)
@row, @col = row, col
@links = {}
end
@jamis
jamis / aldous-broder.rb
Created December 31, 2010 03:10
An implementation of the Aldous-Broder algorithm for generating mazes.
# --------------------------------------------------------------------
# An implementation of Aldous-Broder's algorithm for generating mazes.
# This is an easy one to implement, but it is also one of the
# "dumbest" (meaning least intelligent) algorithms. It is not even
# guaranteed to finish, if you get really unlucky with the RNG.
# Watching the animation of its progress can be an exercise in
# frustration as you find yourself urging the cursor to JUST GO
# OVER THERE! Try and it see for yourself. :)
# --------------------------------------------------------------------
# NOTE: the display routine used in this script requires a terminal
@jamis
jamis / change-x-for-y.rb
Created March 12, 2016 16:43
A script for searching a dictionary file for words that are relatable via a simple text substitution
# Inspired by a church billboard that read:
# "When I becomes we, illness becomes welness"
#
# usage:
# > ruby change-x-for-y.rb i we
# illness wellness
# inch wench
# it wet
# ...