Skip to content

Instantly share code, notes, and snippets.

$('[id|=progress-item]').click(function(event) {
var id = $(this).attr('id');
reader.moveToArticle(id.substr(id.lastIndexOf('-') + 1));
event.stopPropagation();
});
@tomstuart
tomstuart / gist:1113745
Created July 29, 2011 12:50
Parametric modules in Ruby
class Printable < Module
def initialize(string)
super()
define_method :print do
puts string
end
end
end
>> class A; include Printable.new('Hello'); end
@tomstuart
tomstuart / gist:1466504
Created December 12, 2011 10:40
FizzBuzz in the lambda calculus in Ruby
>> IF = -> b { b }
=> #<Proc:0x007fb4e4049cc8 (lambda)>
>> LEFT = -> p { p[-> x { -> y { x } } ] }
=> #<Proc:0x007fb4e403d680 (lambda)>
>> RIGHT = -> p { p[-> x { -> y { y } } ] }
=> #<Proc:0x007fb4e4028ff0 (lambda)>
>> IS_EMPTY = LEFT
-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][
@tomstuart
tomstuart / highlight.rb
Last active December 10, 2015 16:38
Syntax highlighting around inline markup
#!/usr/bin/env ruby
require 'nokogiri'
def highlight(document)
document.xpath('descendant::*[self::programlisting or self::screen][attribute::language]').each do |element|
tokens_and_types = tokenize(element.content, element[:language])
next_token, next_type = tokens_and_types.shift
element.xpath('descendant::text()').each do |text|
MACHINE_RULEBOOK = DTMRulebook.new([
TMRule.new(:delete_next_bit, '0', :increment_counter, 'X', :left), # cross out 0 from input
TMRule.new(:delete_next_bit, '1', :increment_counter, 'X', :left), # cross out 1 from input
TMRule.new(:delete_next_bit, '_', :clean_up, '_', :left), # clean up the tape
TMRule.new(:delete_next_bit, 'X', :delete_next_bit, 'X', :right), # scan right
TMRule.new(:delete_next_bit, 'Z', :delete_next_bit, 'Z', :right), # scan right
TMRule.new(:increment_counter, '1', :increment_counter, 'Z', :left), # increment counter, carry the 1
TMRule.new(:increment_counter, 'Z', :delete_next_bit, '1', :right), # increment counter
TMRule.new(:increment_counter, '_', :delete_next_bit, '1', :right), # increment counter (overflowed)
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape (1)1010>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape (_)X1010>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1(X)1010>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1X(1)010>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape 1(X)X010>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape (1)XX010>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape (_)ZXX010>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1(Z)XX010>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1Z(X)X010>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1ZX(X)010>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape (1)1111>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape (_)X1111>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1(X)1111>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1X(1)111>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape 1(X)X111>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape (1)XX111>>
#<struct TMConfiguration state=:increment_counter, tape=#<Tape (_)ZXX111>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1(Z)XX111>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1Z(X)X111>>
#<struct TMConfiguration state=:delete_next_bit, tape=#<Tape 1ZX(X)111>>
@tomstuart
tomstuart / gist:6782733
Created October 1, 2013 18:18
Downloading and scraping an HTML table
require 'uri'
require 'net/http'
require 'nokogiri'
uri = URI.parse('http://en.wikipedia.org/wiki/Doctor_Who')
html = Net::HTTP.get(uri)
document = Nokogiri::HTML.parse(html)
tables = document.css('table.wikitable')
table = tables.first
@tomstuart
tomstuart / figtest.md
Last active January 2, 2016 01:28
Running a basic Ruby app with Fig on OS X

You must already have pip (brew install python), Vagrant (from the OS X installer, not the gem) and VirtualBox installed.

First, install Docker:

$ curl https://raw.github.com/noplay/docker-osx/master/docker > /usr/local/bin/docker
$ chmod +x /usr/local/bin/docker
$ docker version