Skip to content

Instantly share code, notes, and snippets.

View cstorey's full-sized avatar

cstorey

View GitHub Profile
@cstorey
cstorey / gist:5129666
Last active December 14, 2015 18:29
Ruby's rather non-deductive behavior difference between lambdas / procs. (Edited for coherency).
1.9.3p0 :004 > require 'pp'
=> true
1.9.3p0 :002 > p = proc { |a| pp proc_args: a }
=> #<Proc:0x0000010090b838@(irb):2>
1.9.3p0 :010 > p.call(1,2)
{:proc_args=>1}
1.9.3p0 :008 > stabby = ->(a) { pp stabby_args: a }
=> #<Proc:0x00000101099410@(irb):8 (lambda)>
1.9.3p0 :009 > stabby.call(1,2)
ArgumentError: wrong number of arguments (2 for 1)
@cstorey
cstorey / hack.patch
Last active December 15, 2015 21:59
Mutant crash on 39cd9d72d of cstorey/srsrb.
Mutant configuration:
Matcher: #<Mutant::Matcher::Method::Instance identification="SRSRB::CardEditorApp#card_models_as_dictionary">
Filter: Mutant::Mutation::Filter::ALL
Strategy: #<Mutant::Strategy::Rspec::Full>
Subject: SRSRB::CardEditorApp#card_models_as_dictionary:/Users/cez/projects/srs-rb/lib/srsrb/rackapp.rb:110
Alive: rspec:noop:SRSRB::CardEditorApp#card_models_as_dictionary:/Users/cez/projects/srs-rb/lib/srsrb/rackapp.rb:110:24ec0 (0.71s)
Killed: rspec:SRSRB::CardEditorApp#card_models_as_dictionary:/Users/cez/projects/srs-rb/lib/srsrb/rackapp.rb:110:3e242 (0.43s)
Killed: rspec:SRSRB::CardEditorApp#card_models_as_dictionary:/Users/cez/projects/srs-rb/lib/srsrb/rackapp.rb:110:5d14b (0.43s)
Killed: rspec:SRSRB::CardEditorApp#card_models_as_dictionary:/Users/cez/projects/srs-rb/lib/srsrb/rackapp.rb:110:3aca1 (0.42s)
Killed: rspec:SRSRB::CardEditorApp#card_models_as_dictionary:/Users/cez/projects/srs-rb/lib/srsrb/rackapp.rb:110:d8c97 (0.45s)
@cstorey
cstorey / gosnow.rb
Created April 12, 2013 10:39
Playing around with id generation inspired by gosnow. And by "inspired by" I mean "a blatant copy of".
require 'zlib'
require 'socket'
require 'thread'
WORKERID_BITS = 10
MAX_WORKER_ID = -1 ^ (-1 << WORKERID_BITS)
SEQUENCE_BITS = 12
MAX_SEQUENCE = -1 ^ (-1 << SEQUENCE_BITS)
SINCE_MILLIS = Time.utc(2012, 1, 1).to_f * 1000
@cstorey
cstorey / gist:5377503
Last active December 16, 2015 04:29
Include a background worker in a heroku web dyno.
web: (while true; do echo MARK $(date --iso-8601=seconds); sleep 5; done) & ruby --version; bin/rackup -Ilib -s puma rackup.ru -p $PORT
# Or slightly more clearly
web: rake resqueue:worker & ruby --version; bin/rackup -Ilib -s puma rackup.ru -p $PORT
@cstorey
cstorey / dump.rb
Last active December 16, 2015 04:49
require 'hamster/hash'
original = Hamster.hash :foo => 42
dumped = Marshal.dump(original)
undumped = Marshal.load(dumped)
STDERR.puts undumped[:foo]
STDOUT.puts dumped
@cstorey
cstorey / cors-lobster.rb
Last active December 16, 2015 17:59
Trivial CORS enabled lobster
1 require 'rack/cors'
2 require 'sinatra'
3
4 get '/' do
5 "Hello world"
6 end
7
8 use Rack::Cors do
9 allow do
10 origins 'localhost:3000', '127.0.0.1:3000',
In [17]: pytz.UTC.localize(datetime.utcnow()).isoformat()
Out[17]: '2013-05-16T17:06:19.394052+00:00'
In [18]: (datetime.utcnow()).isoformat()
Out[18]: '2013-05-16T17:06:24.842408'
In [19]:
@cstorey
cstorey / logger_proxy.rb
Last active December 17, 2015 12:19
Lock free Proxy class for the ruby stdlib Logger that can be used from within a signal handler. Not 100% ready for production use, as it cannot currently recover from failures of the logger thread. Lock free queue implementation based on http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue
require_relative 'mpsc'
class LoggerProxy
def initialize target
@target = target
@queue = MpscQueue.new
end
def start!
@reader, @writer = IO.pipe
@cstorey
cstorey / resrv.c
Created June 28, 2013 20:27
Random reservoir sampler in C. Lacks a good deal of proper error checking.
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int size = atoi(argv[1]);
char **resrv = calloc(size, sizeof(char*));
int i = 0; size_t nread;
char *line = NULL;
@cstorey
cstorey / dbcleaner.rb
Last active December 20, 2015 11:49
A trivial Sequel database cleaner.
require 'sequel'
require 'tsort'
class TableCleaner
def initialize db, excluded_tables
@db = db
@excluded_tables = excluded_tables
end
def clean