Skip to content

Instantly share code, notes, and snippets.

View maxjustus's full-sized avatar

Max Justus Spransy maxjustus

  • People With Jetpacks
  • Los Angeles, CA
View GitHub Profile
@maxjustus
maxjustus / locking_methods_bench.rb
Last active November 10, 2015 19:50
Benchmark for db locking approaches
require 'benchmark/ips'
carts = 10.times.map { Cart.create! account: Account.first }
carts.each { |cart|
items = Account.first.product_options.sample(5)
items.each { |item| cart.line_items.create product_option_id: item.id, quantity: 1 }
}
def all_at_once(carts, &blk)
@maxjustus
maxjustus / ranked_sample.rb
Last active September 25, 2015 18:38
Ranked array sample thingey
class Array
def ranked_sample(count)
slice_size = length / count
count.times.reduce([]) do |a, i|
i += 1
to_take = i == count ? length : slice_size * i
v = (take(to_take) - a).sample
a << v if v
a
end
@maxjustus
maxjustus / benchmark_output
Last active September 10, 2015 22:04
Quickly finding count of common elements in same length arrays in Ruby using RubyInline
Calculating -------------------------------------
ruby version 369 i/100ms
c version 3413 i/100ms
-------------------------------------------------
ruby version 3977.2 (±4.5%) i/s - 19926 in 5.020612s
c version 450675.5 (±9.4%) i/s - 2221863 in 4.986029s
~/dev/theclymb$ ./bin/rails runner bench.rb
Calculating -------------------------------------
ruby version 360 i/100ms
c version 3385 i/100ms
@maxjustus
maxjustus / rspec fire active record haxe.rb
Created September 5, 2015 17:01
hack to allow rspec fire to play nice with active record model column methods
# Rspec fire can only mock methods that are defined on a class.
# The way active record defines accessors is incompatible with rspec-fire's
# implementation so this works around that by defining an accessor for every
# attribute as soon as each model class is loaded for the first time
#
# TODO: See if this is needed with Rails 4
class ActiveRecord::Base
class << self
alias :__ar_original_inherited__ :inherited
@maxjustus
maxjustus / gist:0b87c0df16db64e365c2
Created June 29, 2015 21:15
Non-negative matrix factorization in Lua
-- Implements the algorithm described here http://www.quuxlabs.com/blog/2010/09/matrix-factorization-a-simple-tutorial-and-implementation-in-python/
-- by translating http://www.quuxlabs.com/wp-content/uploads/2010/09/mf.py_.txt into the equivalent lua code (I hope)
--
-- INPUT:
-- R : a matrix to be factorized, dimension N x M
-- P : an initial matrix of dimension N x K
-- Q : an initial matrix of dimension M x K
-- K : the number of latent features
-- steps : the maximum number of steps to perform the optimisation
-- alpha : the learning rate
@maxjustus
maxjustus / gist:9339313
Last active August 29, 2015 13:56 — forked from sdliv/gist:9339247
class CreateResumeDesigns < ActiveRecord::Migration
def up
change_table :resume_designs do |t|
t.string :purpose
end
end
def down
end
end
@maxjustus
maxjustus / gist:5723090
Last active December 18, 2015 04:09
Copy and paste in Vim and TMux on OSX

Run the following in your shell:

brew install macvim --override-system-vim
brew upgrade tmux
brew install reattach-to-user-namespace
echo 'set-option -g default-command "reattach-to-user-namespace -l bash"' >> ~/.tmux.conf

@maxjustus
maxjustus / s_stub.rb
Last active December 17, 2015 05:09
safer stubs
module RSpec::Mocks::Methods
# Validates the method exists on the stub-ee before stubbing
def s_stub(message_or_hash, opts={}, &block)
if Hash === message_or_hash
message_or_hash.each {|message, value| s_stub(message).and_return value }
else
raise "#{message_or_hash} not defined on #{self.inspect}" unless self.respond_to?(message_or_hash.to_sym)
stub(message_or_hash.to_sym, opts, &block)
end
end
@maxjustus
maxjustus / my.cnf
Last active December 16, 2015 16:18
# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
# Configuration name mysql55-master generated for mike@theclymb.com at 2012-06-23 04:45:09
@maxjustus
maxjustus / saybot.rb
Last active December 15, 2015 14:29
A campfire bot that speaks the posts aloud. A unique voice is given to each user based on their name.
require 'tinder'
require 'time'
campfire = Tinder::Campfire.new 'subdomain', :token => 'herp'
VOICES = [
'Agnes',
'Albert',
'Alex',
'Bad News',