Skip to content

Instantly share code, notes, and snippets.

View pmarreck's full-sized avatar

Peter Marreck pmarreck

  • formerly senior engineer @ desk.com, chief engineer @ thredup.com, software engineer @ lifebooker.com. Director of Engineering @ addigence.com, currently available
  • Long Island, NY
  • 00:01 (UTC -04:00)
  • X @pmarreck
  • LinkedIn in/petermarreck
View GitHub Profile
@pmarreck
pmarreck / ruby class munging.rb
Created January 30, 2013 17:24
futzing around with changing what the inheritance of a class is. (objects made with the old class will still use the old class' inheritance, though)
bash>> irb
class A;>> class A; end
=> nil
>> class B; end
=> nil
>> the_class = A
=> A
>> class C < the_class
>> end
=> nil
@pmarreck
pmarreck / deferred_includes_and_superclass.rb
Created February 1, 2013 21:27
Problem: You want to be able to load a class without also having to load all its dependencies as a prerequisite, such as in a unit test.
# Problem: You want to be able to load a class without also having to load all its
# dependencies as a prerequisite, such as in a unit test
# test module
module BucketOMethods
def hi
puts 'hi'; 'hi'
end
end
@pmarreck
pmarreck / wtf.rb
Created February 3, 2013 21:18
ruby, wtf
>> a=lambda{:this};b=lambda{:this};a==b
=> true
>> a=lambda{:this}
=> #<Proc:0x007f9d5a8c4430@(irb):24 (lambda)>
>> b=lambda{:this}
=> #<Proc:0x007f9d5a8bade0@(irb):25 (lambda)>
>> a==b
=> false
@pmarreck
pmarreck / inconceivable.rb
Last active December 12, 2015 04:39
an insurmountable bundler problem?
Bundler could not find compatible versions for gem "bundler":
In Gemfile:
rails (= 3.0) ruby depends on
bundler (~> 1.0.0) ruby
Current Bundler version:
bundler (1.2.3)
This Gemfile requires a different version of Bundler.
Perhaps you need to update Bundler by running `gem install bundler`?
@pmarreck
pmarreck / branch_specific_db.yaml
Created February 15, 2013 16:31
A little database.yml hack to get git-branch-specific databases. As a prereq you need the git-branch.bash script that will define parse_git_branch which returns your current git branch (found elsewhere).
<%
db_name = nil
if ENV['SPECIFIC_DB']
db_name = ENV['SPECIFIC_DB'].strip
else
cur_branch = `source ~/bin/git-branch.bash; parse_git_branch`.strip
db_name = cur_branch
end
db_name = db_name.gsub(/[\/\.]/,'_')
test_db = db_name + '_test' + ENV['TEST_ENV_NUMBER'].to_s
@pmarreck
pmarreck / enumerable_self_referential_detect.rb
Created February 19, 2013 16:59
Detect a recursive data structure in Ruby
module Enumerable
def recursive?(oids = {})
if oids.key?(self.object_id)
return true
else
oids[self.object_id] = 1
return any?{ |v| v.recursive?(oids) if v.respond_to?(:recursive?) }
end
return false
end
@pmarreck
pmarreck / make.log
Created February 25, 2013 21:08
Can't build ruby 2.0
[2013-02-25 15:41:08] make
CC = /usr/local/bin/gcc-4.2
LD = ld
LDSHARED = /usr/local/bin/gcc-4.2 -dynamic -bundle
CFLAGS = -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Werror=pointer-arith -Werror=write-strings -Werror=declaration-after-statement -Werror=shorten-64-to-32 -Werror=implicit-function-declaration -pipe
XCFLAGS = -include ruby/config.h -include ruby/missing.h -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT -fPIE
CPPFLAGS = -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -I/Users/pmarreck/.rvm/usr/include -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 -L/Users/pmarreck/.rvm/usr/lib -L/Users/pmarreck/.rvm/usr/lib -fstack-protector -Wl,-u,_objc_msgSend -pie
SOLIBS =
compiling main.c
@pmarreck
pmarreck / lambda-ize_scopes.rb
Created February 27, 2013 18:06
A search/replace regex (this one works in Sublime) to help convert Rails ActiveRecord non-lambda scopes into lambda scopes
# Search for:
/,\s((?:where|order|select)\(.+\))/
# Replace with:
', lambda{ \1 }'
@pmarreck
pmarreck / ruby_array_insert_every.rb
Last active December 14, 2015 14:58
Array#insert_every(skip, elem) An insert_every method for Ruby arrays.
class Array
def insert_every!(skip, str)
orig_ary_length = self.length
new_ary_length = orig_ary_length + ((orig_ary_length-1) / skip)
i = skip
while(i<new_ary_length) do self.insert(i,str); i+=(skip+1) end
self
end
end
@pmarreck
pmarreck / roman_numeral.rb
Last active December 14, 2015 14:59
Roman numerals in Ruby
class String
def to_arabic_numeral
if self =~ RomanNumeral::REGEXP
# The following is some insane magic I won't even touch, but it would work
# n=s=0;bytes{|c|s+=n-2*n%n=10**(205558%c%7)%9995};s+n
RomanNumeral.get(self)
else
raise "String is not a Roman numeral"
end
end