Skip to content

Instantly share code, notes, and snippets.

class Jaccard
def self.coefficient(a,b)
result = (a & b).length / (a + b).uniq.length.to_f
(result * 1000).round / 1000.0
end
end
# Example use:
# weigh_by_relative_importance(@report.items, :cell_value, [2,4,6,8,10]) do |item,weight|
# xml.movie :lat => item.latitude, :long => item.longitude, :title => item.city,
# :height => weight, :width => weight
# end
# Code inspired by http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/
# and meant to be usable for items on a map (as above) or tags
module MapHelper
def weigh_by_relative_importance(items, method_name, classes)
sorted = items.collect {|item| item.send(method_name)}.sort
require 'rubygems'
require 'mechanize'
postal_codes = File.open("postal_codes.txt").read.split("\n")
# randomize to make the pattern slightly harder to see in logs
postal_codes = postal_codes.sort_by {|e| rand(10_000)}
def filename(postcode)
postcode.sub(' ', '')
end
class KeyValuePairs
# Take a file like:
# G6C=24034
# H9E=24049
# and create the hash you'd expect
# if instead you have something like:
# G6C=24034,24036
# H9E=24049
# then use a block to do the specify the value
def self.hash(file_name, separator='=', &block)
class String
def starts_with?(str)
self =~ /#{str}/
end
end
names_to_edid = File.open(File.dirname(__FILE__) + '/edid_names').read.split("\n").
collect {|line| edid, pruid, edname = line.split("|"); [edname.strip, edid.strip]}.
inject({}) {|hash,e| hash[e.first] = e.last; hash }
class Hash
def +(other)
(self.keys + other.keys).uniq.each do |key|
if [self[key], other[key]].all?
if self[key].is_a?(Hash)
self[key] += other[key]
elsif self[key].respond_to?(:+)
self[key] += other[key]
else
raise "values for #{key} were neither hashes or summable"
Dir.glob("*").each do |pc|
File.open(pc).read =~ /aug08cpocRidingProfileTitle>([^<]*)</
puts "#{pc},#{$1}"
end
require 'rubygems'
require 'mechanize'
postal_codes = File.open("postal_codes.txt").read.split("\n")
# randomize to make the pattern slightly harder to see in logs
postal_codes = postal_codes.sort_by {|e| rand(10_000)}
def filename(postcode)
postcode.sub(' ', '')
end
require 'rubygems'
require 'mechanize'
postal_codes = File.open("postal_codes.txt").read.split("\n")
# randomize to make the pattern slightly harder to see in logs
postal_codes = postal_codes.sort_by {|e| rand(10_000)}
@agent = WWW::Mechanize.new do |a|
a.user_agent_alias = 'Mac Safari'
a.max_history = 1
require 'rubygems'
require 'mechanize'
postal_codes = File.open("postal_codes.txt").read.split("\n")
# randomize to make the pattern slightly harder to see in logs
postal_codes = postal_codes.sort_by {|e| rand(10_000)}
@agent = WWW::Mechanize.new do |a|
a.user_agent_alias = 'Mac Safari'
a.max_history = 1