Skip to content

Instantly share code, notes, and snippets.

Peter Marreck pmarreck

  • formerly desk.com, thredup.com and lifebooker.com. currently a contractor
  • Long Island, NY
  • Sign in to view email
Block or report user

Report or block pmarreck

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:278218
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.
# ...
View gist:302950
##### 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
View local_paperclip_tempfile.rb
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
View Sorting accented characters in Ruby correctly.rb
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
View gist:840028
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
View attempts to hack division by zero.rb
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 Jun 10, 2011
Time string formatting
View Time.rb
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 Jul 13, 2011
Homebrew mysterious fail
View gist:1081207
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 Sep 22, 2011
A little code to validate your entire repo for proper UTF-8 encoding.
View Ruby utf-8 Validator
#!/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 Sep 23, 2011
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
View fake_time_machine.sh
#!/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" ]
You can’t perform that action at this time.