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 ThreadsafeCounter | |
include Comparable | |
def initialize(initial = 0) | |
@mutex = Mutex.new | |
@count = initial + 0 | |
end | |
def incr | |
@mutex.synchronize { @count += 1 } |
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 'iconv' unless "".respond_to?(:encode) | |
require 'active_support/basic_object' | |
# = EncodeIo | |
# | |
# This is essentially a basic delegation class. The reason it isn't | |
# using the delegate library from stdlib is that FasterCSV has special | |
# casing for io classes. In order to keep using this, we need #kind_of? | |
# and #class to pretend that this is the original class. | |
# |
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
# = MONKEY PATCH: Memoize Mysql2 Columns | |
# | |
# Reduces SHOW FIELDS selects in production to essentially 0 calls. | |
# | |
# == Reason: | |
# | |
# * We have some pages that are (with rails 3.0.12) generating nearly 1200 SHOW | |
# FIELDS sql statements. | |
# * These come from ActiveRecord::Associations during complex join semantics. | |
# * Esentially, through some relations, Arel::Table instances don't have |
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
# CAVEATS: only known to support 1.8.15 and master as of 2012/2/1 | |
if Gem::Specification.respond_to?(:find_by_name) | |
if Gem.respond_to?(:unresolved_deps) | |
def unresolved | |
Gem.unresolved_deps | |
end | |
elsif Gem::Specification.respond_to?(:unresolved_deps) | |
def unresolved | |
Gem::Specification.unresolved_deps |
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 Uri | |
module Escaping | |
RESERVED = [] | |
%<:/?#[]@!$&'()*+,;=>.each_byte { |b| RESERVED << b } | |
ESCAPE_MAP = Hash.new do |h,k| | |
h[k] = RESERVED.include?(k) ? '%%%02X' % k : k.chr | |
end | |
0.upto(255) { |i| RESERVED[i] } |
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 ObjectSpace.preload | |
starting_size = $".size | |
ObjectSpace.each_object do |o| | |
o.respond_to?(:constants) && constants.each { |c| o.const_get(c) } | |
end | |
retry unless $".size == starting_size | |
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
class A | |
def initialize klass | |
klass.class_eval do | |
def a; end | |
end | |
end | |
new self | |
allocate.a | |
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
require 'rubygems' | |
require 'mechanize' | |
require 'rack/utils' | |
require 'net/http/persistent' | |
require 'yajl' | |
class PagerDutyApi | |
class ApiError < StandardError | |
attr_reader :request, :response | |
def initialize request, response |
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 'sinatra' | |
require 'eventmachine' | |
get '/' do | |
stream(:keep_open) do |out| | |
count = 0 | |
timer = EM.add_periodic_timer(1) do | |
out << "<p>ohai</p>\n" | |
count += 1 | |
if count == 10 |
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
>> begin; raise 'boom'; rescue; end | |
=> nil | |
>> begin; raise 'boom'; rescue => e; end | |
=> #<RuntimeError: boom> |