Skip to content

Instantly share code, notes, and snippets.

@sergueif
sergueif / cache.rb
Created May 8, 2012 04:03
Cache decoupled from persistance
class CachedComputation
attr_accessor :persist, :retreive, :compute, :cache
def [](key)
prep
if result = @retreive[key]
result
else
result = @compute[key]
@persist[key, result]
@sergueif
sergueif / chain.rb
Created April 14, 2012 02:25
message/block chaining
#ChronicDuration is just a random library that parses/outputs strings of time
#The meat of this the last expression/statement
%w{foo bar baz} # ["foo", "bar", "baz"]
ChronicDuration.parse("1:30") # 90
ChronicDuration.output(90) # "1:30"
class Object
def pat(&block) # backwards of 1.9.2 method "tap" but arbitrary name
guard 'rspec' do
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
end
@sergueif
sergueif / test4
Created July 15, 2011 19:01
30 trackers 100 targets
30
79.01, -146.66
43.64, -131.04
73.65, -74.58
-94.01, 86.07
136.93, -109.06
-135.06, 156.47
-152.41, 128.86
131.16, -111.68
-107.87, -162.49
@sergueif
sergueif / test3
Created July 15, 2011 17:21
test3 (10 trackers, 100 targets)
10
153.66, 178.35
-69.98, 177.80
-40.43, -190.60
40.81, 146.07
-151.05, -117.09
197.53, 145.16
-188.28, -100.28
34.92, 16.42
-194.16, -159.82
@sergueif
sergueif / test2
Created July 15, 2011 16:17
test2
17
94.94, -28.18
76.47, -113.90
-120.14, -181.20
-115.41, 93.00
-35.11, 59.41
36.24, -179.82
-111.93, 8.18
-127.18, -80.15
80.93, 117.31
@sergueif
sergueif / kvs.rb
Created June 8, 2011 22:21
Simplest Key-Value store in Ruby
require 'pstore'
class KVS
def self.open(path)
KVS.new(path)
end
def initialize(path)
@pstore = PStore.new path
end
@sergueif
sergueif / dir_ext
Created June 8, 2011 22:16
Methods I wish Dir had
class Dir
def self.current
Dir.new(Dir.pwd)
end
def children
p = path
entries.reject{|f| f =~ /^\..*/ }.map do |name|
child_path = p + "/" + name
if File.directory? child_path
#environment.rb
require 'active_record'
require 'sqlite3'
require 'logger'
ActiveRecord::Base.configurations = YAML::load(IO.read('config/database.yml'))
ActiveRecord::Base.establish_connection('development')
ActiveRecord::Base.logger = Logger.new('log/debug.log')
development:
adapter: sqlite3