Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
@raggi
raggi / fiber_race.rb
Created September 15, 2011 23:34
An example race condition using fibers
## SCROLL DOWN FOR THE RACE, THE FOLLOWING IS JUST UTILITY FROM COMMONLY USED CODE.
# Author:: Mohammad A. Ali (mailto:oldmoe@gmail.com)
# Copyright:: Copyright (c) 2008 eSpace, Inc.
# License:: Distributes under the same terms as Ruby
require 'fiber'
class Fiber
@raggi
raggi / pro_fibers.rb
Created September 27, 2011 17:15
more webscale sekrets
class ShardedFiber < Thread
end
class MongoFiber < Thread
end
class FiberXP < Thread
end
class FiberUltimateEdition < Thread
@raggi
raggi / escape_like.rb
Created September 27, 2011 21:31
YOU WON'T (escape likes)
module EscapeLike
# Escape SQL LIKE arguments. N.B. This should be combined with use of the
# ESCAPE parameter also. See:
# http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
#
# pattern = escape_like(params[:pattern]) << '%'
# query = where(["name LIKE ? ESCAPE ?", pattern, '\\'])
#
# By default this method uses MySQLs default escape, backslash.
# Unfortunately, this can get exceedingly confusing in output. As soon as
@raggi
raggi / wtf.irb.rb
Created October 26, 2011 16:53
wtfbbq ruby?
>> begin; raise 'boom'; rescue; end
=> nil
>> begin; raise 'boom'; rescue => e; end
=> #<RuntimeError: boom>
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
@raggi
raggi / pager_duty_api.rb
Created November 23, 2011 19:07
An API class for PagerDuty
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
class A
def initialize klass
klass.class_eval do
def a; end
end
end
new self
allocate.a
end
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
@raggi
raggi / rfc3986.rb
Created December 23, 2011 19:51
RFC3986.rb
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] }
# 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