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 / infinite_hash.rb
Created June 25, 2012 18:49
Infinitely nested hash (for caching and such)
module InfiniteHash
def self.new
Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
end
end
@maxjustus
maxjustus / gfr.rb
Created August 24, 2012 16:54
Global find and replace with optional confirm
#! /usr/bin/ruby
require 'rubygems'
require "highline/system_extensions"
include HighLine::SystemExtensions
orig, new, args = ARGV
args = Array(args)
file_names = Dir["**/*.*"]
@maxjustus
maxjustus / .vimrc
Created September 28, 2012 17:04 — forked from brycemcd/.vimrc
tmux and vim integration
command! -nargs=1 Silent
\ | execute ':silent !'.<q-args>
\ | execute ':redraw!'
" test the current file in the next tmux window
nmap <leader>tt :Silent tmux send-keys -t 1 "rspec %" C-m <cr>
" test the line from user input
nmap <leader>tl :Silent tmux send-keys -t 1 "rspec %:<C-r>=line('.')<cr>" C-m <cr>
@maxjustus
maxjustus / bulk_insert.rb
Last active March 14, 2018 17:38
Simple SQL bulk insert in Rails
# Takes a table name, and an array of hashes where keys are column names and values are values.
# Expects hashes to all contain the same keys.
def bulk_insert(table, rows)
columns = rows.first.keys
to_insert = rows.map do |d|
vals = columns.map {|k| d[k] }
ActiveRecord::Base.send(:replace_bind_variables, "(#{vals.length.times.collect {'?'}.join(',')})", vals)
end
describe ".inc_num_runs_by_brand" do
let (:upc_master) {FactoryGirl.create(:upc_master_with_sizes)}
it "increments the num runs correctly" do
UpcMaster.inc_num_runs_by_brand(upc_master.sizes.first.brand)
upc_master.reload.num_runs.should == 1
end
end
@maxjustus
maxjustus / gist:5239024
Created March 25, 2013 17:43
OSX built-in text nav
Control-A: Go to the beginning of the line
Control-E: Go to the end of the line
Control-P: Go up to the next line
Control-N: Go down to the next line
Control-F: Go forward one character
Control-B: Bo backward one character
Control-K: Delete all text proceeding cursor
Control-O: Add a line after current line
@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',
@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 [email protected] at 2012-06-23 04:45:09
@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 / 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