Skip to content

Instantly share code, notes, and snippets.

View avescodes's full-sized avatar

Avery Quinn avescodes

View GitHub Profile
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
require 'brewkit'
class Ctags <Formula
@url='http://downloads.sourceforge.net/project/ctags/ctags/5.8/ctags-5.8.tar.gz'
@homepage='http://ctags.sourceforge.net'
@md5='c00f82ecdcc357434731913e5b48630d'
def install
system "./configure", "--prefix=#{prefix}", "--disable-debug", "--disable-dependency-tracking"
system "make install"
#!/usr/bin/env ruby
puts `./simulator ./test/test#{ARGV[0]}.o ./test/test#{ARGV[0]}.dat`