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
  • 03:47 (UTC -04:00)
View GitHub Profile
@pmarreck
pmarreck / Time.rb
Created June 10, 2011 20:51
Time string formatting
class Time
def format_compact(precision = :second, inc_utc = true)
prec = [:year, :month, :day, :hour, :minute, :second].index precision
fmt_a = ["%Y","%m","%d","_%H","%M","%S"][0..prec]
fmt = fmt_a.join('')
self.strftime("#{fmt}#{'%Z' if inc_utc}")
end
def format_datestamp(inc_utc = true)
self.strftime("%Y%m%d#{'%Z' if inc_utc}")
end
@pmarreck
pmarreck / gist:1081207
Created July 13, 2011 20:10
Homebrew mysterious fail
lifebooker@lifebookers-Mac-mini-2[:~[$ /usr/bin/env HOMEBREW_TEMP=/Users/lifebooker/Developer/tmp /Users/lifebooker/Developer/bin/brew install wget
==> Downloading http://ftp.gnu.org/gnu/wget/wget-1.12.tar.bz2
File already downloaded in /Users/lifebooker/Library/Caches/Homebrew
==> Downloading patches
######################################################################## 100.0%
==> Patching
patch unexpectedly ends in middle of line
/usr/bin/patch: **** Only garbage was found in the patch input.
Error: Failure while executing: /usr/bin/patch -f -p1 -i 001-homebrew.diff
lifebooker@lifebookers-Mac-mini-2[:~[$
@pmarreck
pmarreck / Ruby utf-8 Validator
Created September 22, 2011 18:15
A little code to validate your entire repo for proper UTF-8 encoding.
#!/usr/bin/env ruby
class Utf8Checker
def initialize(path = `pwd`.chomp)
@files = Dir["#{path}/**/**"]
@total_num = @files.count
puts "#{@total_num} files to process."
@num_left = @total_num
@valid = false
@err = false
@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 / 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 / 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