Skip to content

Instantly share code, notes, and snippets.

View caius's full-sized avatar
👻

Caius Durling caius

👻
View GitHub Profile
@caius
caius / mutation_detection.rb
Created July 1, 2012 06:42 — forked from peterc/mutation_detection.rb
Argument mutation detection in Ruby (rough experiment)
# encoding: utf-8
# This is a very early scrappy step in building an automatic memoizer (think IncPy)
# in pure Ruby. Is it possible? Who knows. A first step, then, is to rule out any
# methods that mutate their parameters.. which I attempt to do here.
module AutoMemoize
CaughtMutation = Class.new(StandardError)
def monitor meffod
class << self
attr_accessor :config
end
def self.app
Application.new classes
end
def self.classes
@classes ||= []
require 'open-uri'
require 'cgi'
url = "http://tech.dir.groups.yahoo.com/group/OneStopCOBOL"
json = open("http://yahoo-group-data.herokuapp.com/api/v1/group/#{CGI.escape(url)}").read
json = json.force_encoding("utf-8") if RUBY_VERSION[/1\.9/]
puts json
@caius
caius / ShowDerivedData.rb
Created November 5, 2011 13:49
A script for showing the derived data folder for an Xcode workspace. Intended to be invoked from a custom behaviour
#!/usr/bin/env ruby
# This script currently only works if you have your derived data in the default location.
# Get the workspace name to search for, or exit with code 1 if no value in envariable
xcodeWorkspacePath = ENV["XcodeWorkspacePath"] || exit(1)
projectName = xcodeWorkspacePath[%r{/(\w+)\.xcodeproj}, 1]
#Get the folders in derived data that could match the workspace
derived_data = File.expand_path("~/Library/Developer/Xcode/DerivedData")
# Glob out the files matching our project
@caius
caius / gist:1340158
Created November 4, 2011 18:54 — forked from ysr23/gist:1340146
Film.find_each do |f|
next if Film_imdb.find_by_title(f.title)
# Try to find it
imdb_search = Imdb::Search.new(f.title)
m = imdb_search.movies.first
# Check we found a movie
unless m
logger.error "couldn't find film for #{f.title.inspect} on IMDB"
next
@caius
caius / class_typed_methods.rb
Created September 16, 2011 18:03 — forked from ashmoran/class_typed_methods.rb
Class typed methods in Ruby
class Class
def define_typed_method(name, types, &block)
define_method(name) do |args|
args.each do |(arg, value)|
raise ArgumentError.new("Wrong class for #{arg}") unless value.is_a?(types[arg])
end
block.call(args)
end
end
end
@caius
caius / escape_xpath_string.rb
Created June 23, 2011 12:49 — forked from ashmoran/escape_xpath_string.rb
escape_xpath_string
# escape_xpath_string("'Allo 'Allo is the dogs'")
# #=> %Q{concat('',"'",'Allo ',"'",'Allo is the dogs',"'",'')}
#
# ... which is fine, but I'd prefer it if it returned ...
#
# #=> %Q{concat("'",'Allo ',"'",'Allo is the dogs',"'")}
def escape_xpath_string(string)
return string unless string.include?("'")
components = string.split("'", -1).map do |component|
"'#{component}'" unless component == ""
def show
# By making it a bang (!) finder it raises RecordNotFound if nothing's found.
@movie = Movie.find_by_channel_id!(params[:id])
# We found something, render it as XML
respond_to do |format|
format.xml { render :xml => @movie }
end
# Oh noes. Nothing found. Tell ze user with a 404.
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('http://xkcd.com/').read)
image = doc.css("div#contentContainer > div#middleContent > div.bd > div.c > div.s > img")
#
# Results:
# X adult(s), Y child(ren) and Z infant(s)
#
# rules: -
# - pluralization hacks in case of 1 of each of them.
# - only show children and infants if they're > 0
# - at least one adult is always present
#
# examples...