This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Someclass | |
... | |
end | |
########## inline test running | |
if __FILE__==$PROGRAM_NAME | |
require_relative '../path_to_test.rb' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################ | |
# A more comprehensive drop-in replacement for Rails' internal link matcher regex | |
# which I invented from scratch... Oh yeah, Adderall is awesome. | |
# Takes about 40% longer to run than the stock auto_link regex in our test suite unfortunately :( | |
# but it accounts for pretty much EVERY linkable URI/URL, and is far more (in my opinion) | |
# readable and maintainable, due to its use of named groups/tokens. | |
# References used to build this: | |
# http://www.faqs.org/rfcs/rfc1738.html | |
# http://tools.ietf.org/html/rfc3986 | |
# http://en.wikipedia.org/wiki/URI_scheme << primary ref, was awesome, great diagram |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Apparently, I am really 'en fuego' today. | |
require 'test/unit' | |
module RubyRegexMeister | |
BALANCED_GROUP_CHECKER = /( | |
(?<non_grouping_char> | |
[^\(\{\[\<\)\}\]\>] | |
){0} | |
(?<parens_group> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# Patch for auto_link method to chop text up into lines first, | |
# speeding up its run time considerably and making it resilient to bad or overly gigantic input | |
if defined?(ActionView::Helpers::TextHelper) && ActionView::Helpers::TextHelper.instance_methods.include?(:auto_link) | |
module ActionView | |
module Helpers | |
module TextHelper | |
unless defined?(slow_auto_link) # we don't want to method chain it twice... |