This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class << self | |
attr_accessor :config | |
end | |
def self.app | |
Application.new classes | |
end | |
def self.classes | |
@classes ||= [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 == "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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... |
NewerOlder