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
  • 23:24 (UTC -04:00)
View GitHub Profile
@pmarreck
pmarreck / accident_cost.rb
Created June 25, 2012 14:34
A "napkin calculation" to figure out how much taxpayer money is saved by evacuating an accident scene with a Chinook ASAP
cars = 20000.0
avg_salary_per_hr = 40.0
accident_visible_hrs_without_chinook = 1.5
accident_visible_hrs_with_chinook = 0.3
idle_gas_gallons_per_hr = 0.25
gas_cost_per_gal = 4.0
total_cost_of_accident_without_chinook = (cars * avg_salary_per_hr * accident_visible_hrs_without_chinook) +
(idle_gas_gallons_per_hr * gas_cost_per_gal * accident_visible_hrs_without_chinook)
@pmarreck
pmarreck / object_space_snapshot.rb
Created July 18, 2012 20:41
Ruby ObjectSpace "snapshotter", useful for detecting things like monkeypatches
# Voilà. A way to detect monkeypatches in Ruby.
class Module
@@_class_instance_method_map = {}
@@_class_singleton_method_map = {}
def self.object_space_snapshot
@@_class_instance_method_map = {}
@@_class_singleton_method_map = {}
ObjectSpace.each_object(Module) do |o|
@@_class_instance_method_map[o] = o.instance_methods(false).map{|m| [m, o.instance_method(m).hash] }
- facebook_post = FacebookPost.new({:site_id => feed.site_id,
- :facebook_feed_id => feed.id,
- :facebook_user_id => facebook_user.id,
- :facebook_id => post.post_id,
- :sender_user_id => nil,
- :from_id => facebook_user.uid,
- :from_name => fb_actor.name,
- :message => (message ? CGI.unescapeHTML(message) : ""),
- :sent_at => Time.at(post.created_time),
- :data => (post.attachment ? post.attachment.to_json : nil),
@pmarreck
pmarreck / cache_block.rb
Created August 1, 2012 15:39
temporary caching control in a Rails 3.1+ world
def with_caching(on = true)
existing, Rails.application.config.action_controller.perform_caching = Rails.application.config.action_controller.perform_caching, on
yield
ensure
Rails.application.config.action_controller.perform_caching = existing
end
@pmarreck
pmarreck / easy_git_bisect.sh
Created August 2, 2012 22:00
A far simpler interface to git bisect, as a bash function
# automated git bisecting. because I hate remembering how to use this.
# ex. usage: git_wtf_happened <ruby testfile> <how many commits back, default 10>
function git_wtf_happened {
git bisect start HEAD HEAD~${2:-10};
git bisect run ruby $1;
git bisect view;
git bisect reset;
}
@pmarreck
pmarreck / string_to_i_nil.rb
Created August 3, 2012 17:29
ruby to_i on string returning nil
# Make to_i on a string return nil if there are no actual valid numbers
# or if it doesn't at least start with a number.
String.class_eval do
unless instance_methods.include?(:to_i_without_nil)
alias to_i_without_nil to_i
def to_i_with_nil(base=10)
if (r=self.to_i_without_nil(base))==0
if /^[0-9]+/ =~ self
r
@pmarreck
pmarreck / git_solution_generator.rb
Created August 6, 2012 23:09
Mock git solution generator
#!/usr/bin/env ruby
#
# File: git_solution_generator.rb
# Created by Peter Marreck on 2012-08-03.
class GitSolutionGenerator
def solutions(n=1, sep=" ")
(1..n).inject(""){|total,x| total+"#{solution}#{sep}"}
end
@pmarreck
pmarreck / ruby_object_quick_initialization.rb
Created August 30, 2012 15:36
Easy Ruby auto-object-initialization with a params hash
# Some code that lets you quickly erect objects and their attributes with a params hash
class InvalidAutoInitializeParamError < StandardError; end
class Object
class << self
alias new_without_auto_initialize new
def new_with_auto_initialize(*args,&block)
a1 = args.first
@pmarreck
pmarreck / notify_when.rb
Created September 18, 2012 17:57
NotifyWhen, a Ruby module you can extend any object with to get on-the-fly tracing of any later method calls on it
# So you want to find out what is messing with your object somewhere deep in your stack...
# Behold, your on-the-fly tracer...
module NotifyWhen
def notify_when(*meths, &callback)
class << self; self; end.instance_eval do
meths.each do |meth|
alias_method "unnotified_#{meth}".to_sym, meth
define_method(meth) do |*args, &block|
callback.yield(*([meth, args, self][0..callback.arity]))
@pmarreck
pmarreck / makefail
Created November 26, 2012 19:24
makefail
cat /Users/pmarreck/.rvm/log/ruby-1.9.3-p327/make.log
[2012-11-26 15:21:42] make
CC = /usr/local/bin/gcc-4.2
LD = ld
LDSHARED = /usr/local/bin/gcc-4.2 -dynamiclib
CFLAGS = -O3 -fno-common -pipe
XCFLAGS = -include ruby/config.h -include ruby/missing.h -fvisibility=hidden -DRUBY_EXPORT
CPPFLAGS = -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -I/Users/pmarreck/.rvm/usr/include -I. -I.ext/include/x86_64-darwin11.4.2 -I./include -I.
DLDFLAGS = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -Wl,-flat_namespace -install_name /Users/pmarreck/.rvm/rubies/ruby-1.9.3-p327/lib/libruby.1.9.1.dylib -current_version 1.9.1 -compatibility_version 1.9.1 -Wl,-u,_objc_msgSend
SOLIBS =