Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
class Binding
def set(hash)
backup = Thread.current[:binding_set]
hash.each do |var, val|
Thread.current[:binding_set] = val
eval(%(#{var} = Thread.current[:binding_set]))
end
ensure
Thread.current[:binding_set] = backup
end
@raggi
raggi / async_sequel.rb
Created January 13, 2010 00:40
Pooled async sequel stab
require 'eventmachine'
require 'sequel'
# I stole this code from em-mysql (tmm1 <3)
module Sequel
class Database
attr_accessor :_async
end
class Dataset
@raggi
raggi / config_general_parser.rb
Created January 13, 2010 11:27
filthy short + simple parser for Config::General.pm style configs
#!/usr/bin/env ruby
file = ARGV.first
abort "Usage: #{File.basename($0)} file.conf" unless file
cfg = []
current_subtree = cfg
parent = cfg
@raggi
raggi / README
Created February 26, 2010 01:43
This is example code for an ML response.
class String
def hash18
# cheeky integer truncate TODO move to bitmask, and method
trunc = lambda { |v| [v].pack('i').unpack('i').first }
key = 0
each_byte do |b|
key = trunc.call(trunc.call(key*65599) + b)
end
trunc.call(key + trunc.call(key >> 5))
end
require 'eventmachine'
class HTTP < EM::Connection
include EM::P::LineText2
RESPONSE = <<-PLAIN
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 12
Connection: keep-alive
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
Complete requests: 1000
HTML transferred: 44000 bytes
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
Complete requests: 1000
HTML transferred: 44088 bytes
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
Complete requests: 1000
HTML transferred: 44088 bytes
raggi@mbk: ~ % ab -q -k -c 10 -n 1000 http://127.0.0.1/ | egrep "HTML|Complete"
require 'thin'
request = "GET / HTTP/1.0\r\nHost: 127.0.0.1:3001\r\nUser-Agent: ApacheBench/2.3\r\nAccept: */*\r\n\r\n"
def rss
system "ps -orss= #{$$}"
end
rss
while true
class A
def example; end
def leak
method(:example)
end
end
a = A.new
a.leak while true
@raggi
raggi / output
Created July 21, 2010 23:02
an example of how to do cross-ruby constant and local binding as a cached method
raggi@mbk: ~ % ruby tiltbinding.rb
info: jruby-1.5.1: jruby 1.5.1 (ruby 1.8.7 patchlevel 249) (2010-06-06 f3a3480) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [x86_64-java]
[:pass, A::B, :block]
info: macruby-0.6: MacRuby version 0.6 (ruby 1.9.0) [universal-darwin10.0, x86_64]
[:pass, A::B, :block]