Skip to content

Instantly share code, notes, and snippets.

@myronmarston
myronmarston / vcr_custom_matcher.rb
Created October 24, 2012 06:52
VCR custom matcher example
VCR.configure do |vcr|
normalized_uri = lambda do |uri|
uri = URI(uri)
path_parts = uri.path.split('/')
# put a deterministic value in place of the non-deterministic username
path_parts[2] = '__username__'
uri.path = path_parts.join('/')
uri.to_s
end
➜ irb
1.9.3p194 :001 > string = "This is a string"
=> "This is a string"
1.9.3p194 :003 > require 'stringio'
=> true
1.9.3p194 :004 > io = StringIO.new(string)
=> #<StringIO:0x007f988402a890>
1.9.3p194 :005 > io.read(2)
=> "Th"
1.9.3p194 :006 > string.slice!(2, -1)
# A sample Gemfile
source "https://rubygems.org"
gem 'typhoeus', git: 'git://github.com/typhoeus/typhoeus.git'
gem 'sinatra'
# From: https://github.com/seomoz/interpol/blob/5108f29eadf52c4401785d86f4bc75f590813792/Rakefile#L42-51
desc "Import the twitter bootstrap assets from the compass_twitter_bootstrap gem"
task :import_bootstrap_assets do
# when the gem installed as a :git gem, it has "-" as a separator;
# when it's installed as a released rubygem, it has "_" as a separator.
gem_lib_path = $LOAD_PATH.grep(/compass[-_]twitter[-_]bootstrap/).first
assets = Dir[File.join(gem_lib_path, '..', 'vendor', 'assets', '**')]
destination_path = File.expand_path("../lib/interpol/documentation_app/public", __FILE__)
@myronmarston
myronmarston / spec_helper.rb
Created October 2, 2012 21:24
How to make specs fail if they are too slow.
RSpec.configure do |c|
{ unit: 0.1, integration: 0.5 }.each do |type, max_time|
c.after(:each, example_group: { file_path: %r|spec/#{type}| }) do
run_time = Time.now - example.metadata.fetch(:execution_result).fetch(:started_at)
if run_time > max_time
raise "Example took too long: #{run_time} seconds (max is #{max_time} seconds)."
end
end
end
end
@myronmarston
myronmarston / memoizing_symbols_benchmark.rb
Created September 15, 2012 17:31
Benchmark showing that memoizing symbols doesn't make sense.
require 'benchmark'
def memoized_symbol
@memoized_symbol ||= :memoized_symbol
end
def raw_symbol
:raw_symbol
end
@myronmarston
myronmarston / shock.rb
Created September 14, 2012 23:13
Alternate pattern for Shock costing from Sandi Metz's GoGaRuCo Talk
class Shock
def intialize(type)
@type = type
end
def cost
coster_for(type).compute
end
private
@myronmarston
myronmarston / defined.rb
Created September 4, 2012 15:33
Weirdness with defined? on method chains
class Foo
def method_missing(name, *args)
puts "in #{name}"
self
end
def respond_to_missing?(name, include_private)
true
end
end
@myronmarston
myronmarston / .rvmrc
Created August 10, 2012 19:22
Testing out using ActiveSupport's inflector w/o monkey patching String
rvm use 1.9.3
@myronmarston
myronmarston / test_it.rb
Created August 7, 2012 02:18 — forked from jrochkind/test_it.rb
VCR, WebMock, HTTPClient weirdness
require 'httpclient'
require 'vcr'
require 'webmock'
require 'test/unit'
# To allow us to do real HTTP requests in a VCR.turned_off, we
# have to tell webmock to let us.
WebMock.allow_net_connect!
VCR.configure do |c|