Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
require "rubygems"
require "opencv"
include OpenCV
class IChat
def available!
tell_me_to "set status to available"
#!/usr/bin/env ruby
### CHANGE THIS TO YOUR SERVER OK?
TUNNEL_ENDPOINT = '[email protected]'
### YOU CAN ALSO USE 'Ethernet' et al.
INTERFACE = 'AirPort'
PORT = 3022
@techbelly
techbelly / ruby_recursion.rb
Created August 3, 2009 01:07
Simple demonstration of potential elegance of recursion.
#/usr/bin/env ruby
# Naive binary search tree implementation, but illustrates my point
# that one of the key elegances of recursive algorithms
# is that the 'shape' of the code closely matches the 'shape'
# of the data.
#
# So the definition of a binary search tree is either
# a) it's empty (nil in the ruby example)
import hashlib
from BitVector import BitVector
class BloomFilter:
def __init__(self,bits,hashes,digest=hashlib.md5):
self.bits = bits
self.hashes = hashes
self.digest = digest
self.check_digest(bits,hashes,digest)
words = (w.strip().lower() for w in open('/usr/share/dict/words'))
#words = "now is the time for all good lal men godo".split()
def charsorted(word):
return "".join(sorted(list(word)))
results = dict()
for word,index in ((word,charsorted(word)) for word in words):
if index not in results:
results[index] = []
#words = (w.strip().lower() for w in open('wordlist.txt'))
words = (w.strip().lower() for w in open('/usr/share/dict/words'))
#words = "now is the time for all good lal men godo".split()
def charsorted(word):
return str(sorted(word))
results = dict()
for word,index in ((word,charsorted(word)) for word in words):
results.setdefault(index,[]).append(word)
import unittest
class Game:
rolls = [0] * 21
current_roll = 0
def roll(self,pins):
self.rolls[self.current_roll] = pins
self.current_roll += 1
@techbelly
techbelly / gist:826881
Created February 15, 2011 00:34
Scraper syntax
#!/usr/bin/env ruby
require 'gouge'
Gouge::Scraper.construct "BBCNewsHome"do
load "http://www.bbc.co.uk/news/"
stories = make_hash('//a[@class="story"]','@href','text()')
stories.each do |h,t|
puts h,t
if t =~ /.*'[^']+'.*/
now_scrape "BBCNewsPage",h
POSTCODE_REGEXP = /([A-Z]{1,2}[0-9R][0-9A-Z]?\s*[0-9][ABD-HJLNP-UW-Z]{2})/i
def parse(string)
address, postcode = string.split(POSTCODE_REGEXP)
town, county = "",""
mode = :seeking_county
collect = ""
@techbelly
techbelly / mapit_mapper.py
Created March 31, 2011 11:08
Maps loose county names to mapit area ids
import json
def mappit_county_matcher(counties_json):
source_counties = json.loads(counties_json)
counties_map = {}
for code in source_counties.values():
name,type_name,type_id,mapit_id = code[u'name'],code[u'type_name'],code[u'type'],code[u'id']
counties_map[name] = {