Skip to content

Instantly share code, notes, and snippets.

View avescodes's full-sized avatar

Avery Quinn avescodes

View GitHub Profile
def process_nethack_log(log)
data = log.scan(/#{"(.*) "*15}(.*),(.*)/)[0]
fields = %W| version score dun_number dun_level max_level hp max_hp deaths end start uid role race gender alignment name ending|
results = {}
(0..16).each do |i|
results[fields[i]] = data[i]
end
results
end
# So Tags has many "Freetaggables", a polymorphic association. Contacts is one of those freetaggables.
# My mentor was working on some old imports and this bug crops up.
# The code below raises an error. The record is invalid for some reason.
# Turns out since the join has a composite key it is failing - it isn't unique. That doesn't seem possible.
# It just so happens there is a really subtle bug where if you don't save both models before associating them you get a duplicate record in the join
def self.parent_select_options
options = [['-- No Parent --', nil]]
Tag.recurse_for_parent_select_options(Tag.roots,options)
return options
end
def self.recurse_for_parent_select_options(nodes,options)
nodes.each do |node|
prefix = node.depth > 0 ? ' -' * node.depth + ' ' : ''
options << [ prefix + node.title, node.id]
@avescodes
avescodes / .vimrc
Created June 27, 2009 10:24
Awesome binding to make Vim work a bit like TextMate (just the good parts)
" Fuzzy Finder Mappings
map <C-t> :FuzzyFinderFileWithCurrentBufferDir
class FreemailerCampaign < ActiveRecord::Base
belongs_to :sender, :class_name => 'User'
has_many :freemailer_campaign_contacts, :dependent => :destroy
has_many :contacts, :through => :freemailer_campaign_contacts
validates_uniqueness_of :title, :scope => :sender_id, :on => :create, :message => "must be unique" #JS me
attr_accessible :title, :subject
before_destroy :remove_active_campaign
def contact_names
def self.ranking_sort(results,query)
results.sort { |a,b| ranking_score(a,query) <=> ranking_score(b,query) }
end
# Compute the two-ordered score for sorting
# Score is of the form [ 1 / # of matches, average distance ]
# This arises because we may have 1 or more query parts that aren't matched at all
# Since those elements return nil we can't exactly set them to 0 or MAX and expect reliable results
# e.g. "Carrot Soup" === ["carrot","soup"] = (0 + 8) / 2 = 4 # Clearly the best result
# "Carrot's, raw" === ["carr...soup"] = (0 + 0?) / 2 = 0 # but setting to 0 breaks this
def ham_dist(word)
a = SHA1.hexdigest(word).to_i(16)
b = "6cac827bae250971a8b1fb6e2a96676f7a077b60".to_i(16)
(a ^ b).to_s(2).split('').inject(0) {|sum,i| sum + i.to_i }
end
def permute(sentence)
non_space_chars = sentence.scan(/\S/).count
(0...(2**non_space_chars)).each do |mask|
yield apply_mask(sentence,mask)
require 'digest/sha1'
def suffix(seqnum)
o = ""
5.times do
o << (33 + (seqnum % 93)).chr
seqnum /= 93
end
return o
end
>> smtp = Net::SMTP.new('oz.law.harvard.edu', 465)
=> #<Net::SMTP oz.law.harvard.edu:465 started=false>
>> smtp.set_debug_output $stderr
=> #<IO:0x126dbc>
>> smtp.start( 'oz.law.harvard.edu', 'rneufeld', MY_PASS, :login)
Connection opened: oz.law.harvard.edu:465
Timeout::Error: execution expired
from /opt/local/lib/ruby/1.8/timeout.rb:60:in `rbuf_fill'
from /opt/local/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from /opt/local/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
# Code Golf: Text Area Tag Edition
# Ladies and Gentlemen here is the function
# Smallest (and least legible) rewrite wins!
def size_for_text_area_tag(string, cols = 40)
lines = string.split("\n")
rows = lines.map(&:length).inject(0) do |sum,len|
sum + if len/cols > 0
len/cols
else