Skip to content

Instantly share code, notes, and snippets.

View pmarreck's full-sized avatar

Peter Marreck pmarreck

  • formerly desk.com, thredup.com and lifebooker.com. currently Director of Engineering @addigence
  • Long Island, NY
  • 00:21 (UTC -04:00)
View GitHub Profile
@pmarreck
pmarreck / fixutf8
Created October 11, 2011 21:54
Fixing the BOM (byte order marker) at the beginning of utf8 files.
#!/usr/bin/env sh
# BOM=`echo -ne '\xEF\xBB\xBF'`
# echo "$BOM$(cat $1)" > $1
(printf '\xEF\xBB\xBF'; cat $1) > $1.enc; mv -f $1.enc $1
@pmarreck
pmarreck / migration_skip_duplicates.rb
Created November 9, 2011 23:02
A little lib to cause migrations to skip duplicates instead of failing.
class MyCompany::Migration < ActiveRecord::Migration
def self.add_column(*args)
begin
super
rescue => e
if e.message =~ /Duplicate/
puts "WARNING, a migration line was skipped due to it already existing: #{e.message}"
else
raise e
end
@pmarreck
pmarreck / rvm traced requirements
Created February 28, 2012 20:04
RVM thinks there is no Xcode, when there is an Xcode (fresh Lion install, fresh Xcode 4.2 install, installed command line tools within Xcode prefs)
pmarreck$ rvm --trace requirements
+ [[ -n '' ]]
+ export 'PS4=+ ${BASH_SOURCE##${rvm_path:-}} : ${FUNCNAME[0]:+${FUNCNAME[0]}()} ${LINENO} > '
+ PS4='+ ${BASH_SOURCE##${rvm_path:-}} : ${FUNCNAME[0]:+${FUNCNAME[0]}()} ${LINENO} > '
+ /scripts/cli : __rvm_parse_args() 769 > [[ -z '' ]]
+ /scripts/cli : __rvm_parse_args() 769 > [[ -n '' ]]
+ /scripts/cli : __rvm_parse_args() 771 > [[ 0 -eq 1 ]]
+ /scripts/cli : __rvm_parse_args() 771 > [[ -n '' ]]
+ /scripts/cli : __rvm_parse_args() 19 > [[ -n requirements ]]
+ /scripts/cli : __rvm_parse_args() 21 > rvm_token=requirements
@pmarreck
pmarreck / First stab at caching of model instances.rb
Created March 14, 2012 20:24
Caching of model instances in Rails.cache
# We have a website with a lot of subdomains, each corresponds to a Site object, and the Site model/DB gets hit A LOT even though it rarely changes.
# We were looking at ways to optimize/cache this.
def current_site(memoize = true)
# (assumes "subdomain" and "domain" are available from URL information, this code may need to be tweaked)
@current_site = nil unless memoize
@current_site ||= Site.allocate.init_with('attributes' =>
Rails.cache.fetch("Site#{subdomain}.#{domain}") do
Site.find_by_subdomain_and_domain(domain, subdomain).attributes
end
@pmarreck
pmarreck / .profile
Created March 15, 2012 19:23
.profile/.bashrc hacks for OS X
# HOMEBREW CONFIG
# Move /usr/local/bin to the front of PATH by subbing it out and prepending
export PATH=/usr/local/bin:${PATH/\/usr\/local\/bin:/}
# add my user bin to PATH
export PATH=$PATH:~/bin
# stop checking for mail
unset MAILCHECK
@pmarreck
pmarreck / rubyhashflip.rb
Created April 3, 2012 18:31
How to get hash format flipping between Ruby 1.8-style and Ruby 1.9-style in Sublime Text 2
# First, install the Sublime package SublimeExternalCommand with the Package Control sublime package.
# Then add the following file in your ~/bin (or somewhere in your PATH, note that you may have to change some paths later here) and call it "rubyhashflip" and chmod +x it:
#!/usr/bin/env ruby
begin
require 'hash_syntax'
rescue LoadError
puts `gem install hash_syntax 2>&1`
@pmarreck
pmarreck / extra_attributes.rb
Created April 4, 2012 20:02
An idea I had to return a single value from a method with "hidden" extra attributes.
# encoding: UTF-8
require 'delegate'
# ExtraAttributes: An object that lets you sneak as many return values as you want
# through a single scalar-looking return value.
# Any return values set are immutable.
# Example usage:
# def some_method
# ExtraAttributes.new(200, debug: 'from some_method')
# end
@pmarreck
pmarreck / string_unindenter.rb
Created April 10, 2012 20:54
My Ruby string unindenter method
# encoding: utf-8
module StringUtils
# Unindents a multiline string,
# even if the first line is not the one with the least whitespace.
# Best used with heredocs like this: <<-EOS.unindent ...
# Note that if there are lines that are just newlines that
# those will not break the next minimum indent length due to (?!\n) in the regex.
# This is to get around editors which auto strip trailing whitespace on lines.
# Otherwise any stripped whitespace on a blank line would kill the unindenting.
@pmarreck
pmarreck / Ruby.sublime-build
Last active December 10, 2023 20:00
Get Sublime Text 2 (or 3) to use your RVM ruby and bundle Gemfile
# Get Sublime to use your rvm ruby... Change your ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build
# (for ST3: ~/Library/Application Support/Sublime Text 3/Packages/User/Ruby.sublime-build) to this, replacing YOURUSERNAME.
# I am still looking to optimize this further... For example, avoiding hardcoding by using something like $HOME, although
# I have yet to get any form of that to work.
{
"working_dir": "${project_path}",
"cmd": [
"/Users/YOURUSERNAME/.rvm/bin/rvm-auto-ruby", "-Ilib:test", "$file"
],
@pmarreck
pmarreck / hash_utils.rb
Created April 20, 2012 19:52
attempt to traverse a hash with a key array
module HashUtils
# Pass in indexes as an array of keys
# def traverse(indexes)
# k = indexes.shift
# unless k.nil?
# v=self[k]
# if v.is_a?(Hash)
# !indexes.empty? ? v.traverse(indexes) : v
# else
# indexes.empty? ? v : nil