Skip to content

Instantly share code, notes, and snippets.

1> Parse = fun(File) ->
1> {ok, Bin} = file:read_file(File),
1> metagrammar:parse(binary_to_list(Bin))
1> end.
#Fun<erl_eval.6.13229925>
2> io:format(Parse("extra/metagrammar.peg")).
rule(rules) ->
peg:seq([peg:optional(fun space/2), fun declaration_sequence/2, peg:optional(fun space/2)]);
rule(declaration_sequence) ->
peg:seq([peg:label('head', fun declaration/2), peg:label('tail', peg:zero_or_more((peg:seq([fun space/2, fun declaration/2]))))]);
@seancribbs
seancribbs / autoconf.rb
Created June 24, 2009 00:11
Definition and library for Chef, allowing automatic installation of packages from source tarballs.
module Autoconf
extend self
def switches(config=nil)
case config
when String
switch(config)
when Enumerable
config.map {|item| switch(item) }.join(" ").strip
else
require 'digest/md5'
# A little HTTP Digest authentication calculator, based on the C code in RFC2617
class DigestAuthCalc < Struct.new(:algorithm, :username, :realm, :password, :nonce,
:cnonce, :nc, :qop, :method, :uri, :entity)
# Calculates the "response" header parameter from the data in the struct
def response
hash_sequence(ha1, nonce) do |resp|
resp << [nc, cnonce, qop] if qop
# From webrat/selenium/application_servers/base.rb
TCPSocket.wait_for_service_with_timeout \
:host => "0.0.0.0",
:port => Webrat.configuration.application_port.to_i,
:timeout => 30 # seconds
def timezone=(value)
# Normalizes TZInfo names into their Rails equivalents
Time.use_zone(value) do
self[:timezone] = ActiveSupport::TimeZone::MAPPING.index(Time.zone.name) || Time.zone.name
end
end
result = Spreedly.post("/subscribers.xml", :body => "<subscriber><screen-name></screen-name><customer-id>1</customer-id><email></email></subscriber>")
=> {}
>> result.code
=> 422
>> result.body
=> "undefined method `delete' for nil:NilClass"
require 'benchmark'
require 'rubygems'
require 'mongo'
require 'mongo_ext/cbson'
SMALL_DOC = {}
MEDIUM_DOC = {"integer" => 5, "number" => 5.05, "boolean" => false, "array" => %w{test benchmark}}
LARGE_DOC = {
"base_url" => "http://www.example.com/test-me",
"total_word_count" => 6743,
# After Array#pack optimizations
insert (small, no index)....................................7398
insert (medium, no index)...................................1591
insert (large, no index)....................................968
insert (small, indexed).....................................5832
insert (medium, indexed)....................................6185
insert (large, indexed).....................................1023
batch insert (small, no index)..............................27654
batch insert (medium, no index).............................17027
The C oid generation uses bare C types and casting to get around the pack/unpack inefficiencies.
Here's 100_000.times { Mongo::ObjectID.new } with each algorithm (using Benchmark.bmbm).
Rehearsal --------------------------------------------
Ruby oid 2.610000 0.350000 2.960000 ( 2.976763)
C oid 0.950000 0.300000 1.250000 ( 1.251934)
----------------------------------- total: 4.210000sec
user system total real
Ruby oid 2.710000 0.330000 3.040000 ( 3.063867)
insert (small, no index)....................................6776
insert (medium, no index)...................................1459
insert (large, no index)....................................1038
insert (small, indexed).....................................5734
insert (medium, indexed)....................................5695
insert (large, indexed).....................................1022
batch insert (small, no index)..............................20394
batch insert (medium, no index).............................14569
batch insert (large, no index)..............................906
find_one (small, no index)..................................831