Skip to content

Instantly share code, notes, and snippets.

View adacosta's full-sized avatar

Alan Da Costa adacosta

  • San Diego, California, United States
View GitHub Profile
@adacosta
adacosta / webrick_race.rb
Last active August 29, 2015 14:12
WEBrick TimeoutHandler race condition
require 'webrick/utils'
# The initialize below is augmented to stall the @timeout_info iteration.
# The fix is uncommenting TimeoutMutex.synchronize{} around the @timeout_info iteration.
class WEBrick::Utils::TimeoutHandler
def initialize
@timeout_info = Hash.new
Thread.start{
while true
now = Time.now
@adacosta
adacosta / string_compare.rb
Created May 25, 2011 21:03
ruby string compare
require 'benchmark'
def repeat(n=10_000_000, &blk); n.times {yield}; end
string = 'string'
Benchmark.bm do |x|
x.report("warm-up single quotes") { repeat {'this is a string'} }
x.report("interpolated double quotes with variable") { repeat {"this is a #{string}"} }
x.report("double quotes") { repeat {"this is a string"} }
x.report("single quotes") { repeat {'this is a string'} }
require 'nokogiri'
require 'open-uri'
require 'uri'
def search(query_string)
Nokogiri::HTML(open("http://www.google.com/search?q=#{URI.encode(query_string)}"))
.css("h3[class=r] a").map {|node| {:url => node["href"], :title => node.inner_html} }
end
puts search('ruby')
var baseline_question_id_by_improvement_plan_option_id = {
// Was a history and physical documented in the medical record?
'4c570625b49164349b000002': '4c05a95eb491640c0600000e',
// Is an AJCC stage in the medical record?
'4c570625b49164349b000003': '4c05a95eb491640c06000011',
// Is the pathology noted in the consultation note?
'4c570625b49164349b000004': '4c05a95eb491640c06000015',
// Was the intent of the treatment documented?
'4c570625b49164349b000005': '4c05a95eb491640c06000015',
// Was the treatment based upon clinical practice guidelines or published data?
# Git aliases
alias g="git"
alias ga="git add"
alias gb="git branch"
alias gs="git status"
alias gm="git merge"
alias grm="git rm"
alias gci="git commit"
alias gca="git commit --amend"
alias gco="git checkout"
# 1. Use two spaces instead of tabs
# 2. Avoid using 'return' when not necessary. It's unnecessary.
# before
def current_user
return session[:user]
end
# after
def current_user
def lambda_mem(f, cache=nil)
cache = {} if cache.nil?
lambda do |h|
return cache[h] if cache[h]
cache[h] = f.call( lambda {|n| lambda_mem(f, cache).call(n) } ).call(h)
end
end
def fib(g)
raise(ArgumentError, "Argument must be positive") if g < 0
module MozRepl
require 'base64'
require 'net/telnet'
PROMPT = /\nrepl>\s/
class Telnet < Net::Telnet
alias_method :old_cmd, :cmd
def cmd(options, &blk)
old_cmd(options, &blk).gsub!(MozRepl::PROMPT, '')
['rubygems', 'rack'].each { |lib| require lib }
app = lambda { |env| [200, {'Content-Type' => 'text/html'}, env.inspect] }
Rack::Handler::WEBrick.run(app, :Port => 4004)
require 'base64'
require 'test/unit'
module B64
CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split ''
# === Encode Non-MIME
# convert string to binary
# copy groups of 8 bits to 6 bits
# add b64 padding - 0 fill (to 6) the right side of the last bit grouping