Skip to content

Instantly share code, notes, and snippets.

@mtkd
mtkd / json-vs-yajl.rb
Created January 7, 2012 16:20
JSON vs Yajl
require 'benchmark'
require 'json'
require 'yajl'
data = ''
f = File.open("test.json", "r")
f.each_line do |line|
data += line
end
@mtkd
mtkd / uuid_benchmark.rb
Created December 24, 2011 22:47
UUID Benchmarks
require 'benchmark'
require 'uuidtools'
require 'ffi-uuid'
require 'uuid'
Benchmark.bm do |x|
x.report { 100000.times do; UUIDTools::UUID.timestamp_create.to_s; end; }
x.report { 100000.times do; UUID.generate.to_s; end; }
x.report { 100000.times do; FFI::UUID.generate_time.to_s; end; }
end
@mtkd
mtkd / Mongoid_id
Created April 23, 2011 10:23
Mongoid ID issue with state_machine
# tl;dr if state_machine is in the model when a record is created, there is a record in Mongo, but you can't do a find() on it
# If state_machine is in the model when the record is created:
ruby-1.9.2-p0 > Upload.first.id
=> BSON::ObjectId('4db2a774c4054453d000000c')
ruby-1.9.2-p0 > BSON::ObjectId.from_string('4db2a774c4054453d000000c')
=> BSON::ObjectId('4db2a774c4054453d000000c')
@mtkd
mtkd / gist:586685
Created September 19, 2010 11:24
Mongoid references_one persistance quirk
User (references_one Address)
Address (referenced_in User)
user = User.make
address = Address.make
user.address = address
user.save # does not work, the change does not persist
user.address.save # works and change persists
@mtkd
mtkd / gist:516046
Created August 9, 2010 20:26
Ruby to get process memory usage
`ps -o rss= -p #{$$}`.to_i
@mtkd
mtkd / gist:516044
Created August 9, 2010 20:24
HTTP Response Codes in Ruby
case code
when 200; "OK"
when 201; "Created"
when 202; "Accepted"
when 203; "Non-Authoritative Information"
when 204; "No Content"
when 205; "Reset Content"
when 206; "Partial Content"
when 300; "Multiple Choices"
when 301; "Moved Permanently"
@mtkd
mtkd / gist:516042
Created August 9, 2010 20:22
Enumerate MongoDB indexes with Mongoid
a = []
Mongoid.master.collections.each do |x|
x.index_information.map do |y|
a.push [y[1]["ns"], y[1]["name"], y[1]["key"].inspect, y[1]["unique"]]
end
end
@mtkd
mtkd / truncated mean in Ruby
Created August 6, 2010 17:51
truncated mean in Ruby
ndrop = v.length/10
nkeep = v.length - 2*ndrop
trunc_mean = v.sort[ndrop,nkeep].sum/(1.0*nkeep)
09.00 Keynote
10.30 Denormalizing Your Rails Application
11.30 I Think I Finally Understand Mocks
12.30 Getting the most out of ActiveRecord 3 with Arel
14.15 Taking the next step in Web Development with Document Databases
15.30 Rack Middleware Goodies
16.30 Distributed Architectures with Rack
09.00 Keynote
10.30 RTW (Real Time Web): WTF (What's That For)?
@mtkd
mtkd / bread_rolls.txt
Created January 6, 2010 11:44
Bread Recipe
Makes 6 regular or 8 smaller, takes about an hour or 40 mins if in a hurry.
For flour you can use white, wholemeal or my favourite: 50% untreated strong white flour (from Garboldisham mill) and 50% Hovis granary.
Even when I make wholemeal I use 50% white or they come out too heavy.
* 500g flour
* 280ml warm water
* 1.5 tsp yeast (like Allinson Dried Active Yeast)
* 1 tsp sugar