Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / keybase.md
Created September 23, 2014 20:18
Verifying my GitHub identity with Keybase.io

Keybase proof

I hereby claim:

  • I am jamis on github.
  • I am jamis (https://keybase.io/jamis) on keybase.
  • I have a public key whose fingerprint is D6BF 2C78 D257 1F45 892A F160 3E22 BCB7 321F F098

To claim this, I am signing this object:

@jamis
jamis / query-parser.rb
Last active October 1, 2019 11:52
A simple example of a BNF-based, recursive-descent parser for reading a field-based query string
require 'strscan'
# expr := term
# | term AND expr
# | term OR expr
# term := value
# | atom ':' value
# atom := word+
# | quoted_string
# value := atom
@jamis
jamis / grid.txt
Created August 4, 2015 22:25
A program for solving Sam Lloyd's "Back from the Klondike" puzzle.
__________xxx__________
_______xxx477xxx_______
_____xx544833463xx_____
____x1451114517135x____
___x494967555876685x___
__x37298356739187585x__
__x14784292711822763x__
_x7218553113133428613x_
_x4267252422543281773x_
_x4165111914344319827x_
@jamis
jamis / maze.erl
Created August 17, 2015 20:05
An implementation of the Recursive Backtracker maze generation algorithm in Erlang
-module(maze).
-export([generate/2,north/3,south/3,west/3,east/3,visualize/1]).
% Generate and return a maze of width W and height H.
generate(W,H) -> try_directions(random:uniform(W)-1, random:uniform(H)-1, directions(), {width,W,height,H,0}).
% Returns true if there is a passage north from the given position in the maze.
north(X,Y,Maze) -> at(X,Y,Maze) band 2#1 =/= 0.
% Returns true if there is a passage south from the given position in the maze.
@jamis
jamis / maze.hs
Last active August 29, 2015 14:27
An implementation of the Recursive Backtracker maze generation algorithm in Haskell
{- --------------------------------------------------------------------------
- A naive random maze generator.
-
- $ ghc -o maze Maze.hs
- $ maze 10 10
-
- Author: Jamis Buck (jamis@jamisbuck.org)
- ------------------------------------------------------------------------ -}
module Main where
@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 } }