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
  • 13:01 (UTC -04:00)
View GitHub Profile
@pmarreck
pmarreck / benchmarks.rb
Created May 17, 2012 15:21
Some interesting benchmark results for various ways of doing typical Ruby things
require 'benchmark'
REP = 1000000
CACHED_REGEX = /mat[ac]?h/
OBJECT_REGEX = Regexp.new('mat[ac]?h')
Benchmark.bm 20 do |x|
x.report 'Regex interpolated string' do
REP.times do |index|
@pmarreck
pmarreck / is_json.rb
Created May 23, 2012 14:57
Apparently this is an actual JSON validator, written in late-model Regexp, using the Ruby interpreter.
# encoding: utf-8
module Constants
JSON_VALIDATOR_RE = /(
# define subtypes and build up the json syntax, BNF-grammar-style
# The {0} is a hack to simply define them as named groups here but not match on them yet
# I added some atomic grouping to prevent catastrophic backtracking on invalid inputs
(?<number> -?(?=[1-9]|0(?!\d))\d+(\.\d+)?([eE][+-]?\d+)?){0}
(?<boolean> true | false | null ){0}
(?<string> " (?>[^"\\\\]* | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9a-f]{4} )* " ){0}
@pmarreck
pmarreck / muddled_middleware_test.rb
Created June 2, 2012 18:04
An attempt to test a railtie which installs rack middlewares
require 'test/unit' unless defined? Test::Unit
# require_relative '../../lib/object_utils' unless defined? ObjectUtils
# require_relative '../../lib/hash_utils' unless defined? HashUtils
# require_relative '../../lib/array_utils' unless defined? ArrayUtils
require 'rack/test' unless defined? Rack::Test
# require 'active_support/all' unless defined? ActiveSupport
# require 'rack'
# require 'action_dispatch'
# require 'action_controller'
require 'ap'
@pmarreck
pmarreck / s3_object.rb
Created June 5, 2012 21:15
AWS programmers whut?
# This is inside a "def write" method in the AWS::S3 gem. There is no error check code whatsoever. On a write, over the wire, to an external resource.
# Amazon can't acknowledge that its stuff goes down sometimes or errors out, apparently... :)
if use_multipart?(data_options, put_options)
put_options.delete(:multipart_threshold)
multipart_upload(put_options) do |upload|
each_part(data_options, put_options) do |part|
upload.add_part(part)
end
end
@pmarreck
pmarreck / in_line_test_code.rb
Created June 8, 2012 17:12
I put this at the end of my class files so that running the file directly runs the test for the file.
class Someclass
...
end
########## inline test running
if __FILE__==$PROGRAM_NAME
require_relative '../path_to_test.rb'
end
@pmarreck
pmarreck / .rvmrc
Created June 14, 2012 19:18
Add your project's load paths to the Ruby load paths, when you're anywhere in that project, via RVM's .rvmrc
# Put this at the end of your PROJECT .rvmrc (not your $HOME/.rvmrc!)
# add this project's paths to the $LOAD_PATH
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export RUBYLIB="$DIR:$DIR/test:$DIR/lib"
@pmarreck
pmarreck / magically_instantiated.rb
Created June 18, 2012 14:45
Fully instantiate an ActiveRecord object, complete with ID, without touching the DB
# We had a situation where the site subdomain object had to be read on every single request and were looking for a way to reduce the DB hits, this worked, although you have to be careful to expire the cache properly if the object in question changes
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 / new_auto_link_regex_constant.rb
Created June 19, 2012 20:37
A URI detector/parser regex that is a better, but slower, auto_link regex for Rails (and a line that patches it in)
@pmarreck
pmarreck / balanced_group_checker_regex.rb
Created June 20, 2012 15:48
A ruby regex to validate any level of nested balanced group characters
# Apparently, I am really 'en fuego' today.
require 'test/unit'
module RubyRegexMeister
BALANCED_GROUP_CHECKER = /(
(?<non_grouping_char>
[^\(\{\[\<\)\}\]\>]
){0}
(?<parens_group>
@pmarreck
pmarreck / actionview_helpers_texthelper_autolink_patch.rb
Created June 20, 2012 23:42
A monkeypatch to Rails' ActionView::Helpers::TextHelper (or that class provided by the rails_autolink gem) which causes it to chop up input data in case it's too long to handle