Navigation Menu

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
  • 02:33 (UTC -04:00)
View GitHub Profile
class Brand < ActiveRecord::Base
(0..5).each do |f|
define_method("flags_#{f}".to_sym) { read_attribute("flags_#{f}".to_sym).to_2s_complement(64) }
define_method("flags_#{f}=".to_sym){ |n| write_attribute("flags_#{f}".to_sym), n.to_2s_complement(64) }
end
# I'm getting syntax errors in the above code. Not sure why.
# ...
##### FILE : lib/words.rb
# Gives you N random word(s) over a certain length
# Only works on unixlike systems where this file exists
class Words
# DICT_PATH = '/usr/share/dict/words'
# DICT_SIZE = 234936
def self.random_word(words = 1, minlength = 6)
@@dict_all ||= File.open("/usr/share/dict/words").readlines.map{|l| l.chomp}
@@dict_size ||= @@dict_all.length
require 'digest/sha1'
require 'tempfile'
class LocalPaperclipTempfile < ::Tempfile
# note: had a ton of trouble using "data" instead of "tmp_data" for naming here:
def initialize(filename, tmp_data, tmpdir = Dir::tmpdir)
@original_filename = filename
@tmp_data = tmp_data
super Digest::SHA1.hexdigest(@tmp_data), tmpdir
class String
# note: very rough, does not handle caps (but that could be added), just something to help sort accented characters correctly...
TRANSLATION_HASH = {"’"=>"'","åäáàâ" => "a", "ëéèê" => "e", "ïíìî" => "i", "öóòô" => "o", "üúùû" => "u", "ß" => "ss"}
UNUNICODE = Hash[*TRANSLATION_HASH.map{|u,as| u.unpack("U*").inject([]){|sum,e| sum << e; sum << as}}.flatten]
def un_unicode
self.unpack("U*").map{|c| UNUNICODE[c] ? UNUNICODE[c] : c.chr}.join
end
end
# use case
require 'sender'
# For fun, trying to define my own string interpolator so that any symbol in a string (optionally end-delimited with a colon if not at a word boundary) gets substituted for its eval'd value.
# Here's what I have so far. No workie, due to context issue with "eval" call. Help?
class String
def i
self.gsub(/:([\w_]+)/){|match| __sender__.eval $1}
end
module Mathhax
def /(o)
if o==0
puts "I'm sorry, #{`hostname`.chomp}, I'm afraid I can't do that"
else
super
end
end
end
@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 / fake_time_machine.sh
Created September 23, 2011 16:29
Fake Time Machine, a way to duplicate the functionality of Apple's Time Machine using rsync, which also might make it cross-platform, but also lets you run it to any remote server
#!/usr/bin/env sh
# fake_time_machine.sh
# Fake Time Machine
# by Peter Marreck
# Based on ideas in http://blog.interlinked.org/tutorials/rsync_time_machine.html
# 9/2011
# E_BADARGS=85
# if [ -z "$1" ]