Skip to content

Instantly share code, notes, and snippets.

@myronmarston
myronmarston / Rakefile
Created September 11, 2013 04:32
Alternate code for ruby tapas #131
source_files = Rake::FileList.new("**/*.md", "**/*.markdown") do |fl|
fl.exclude("~*")
fl.exclude(/^scratch\//)
fl.exclude do |f|
`git ls-files #{f}`.empty?
end
end
task :default => :html
task :html => source_files.ext(".html")
@myronmarston
myronmarston / librato-repro-script.rb
Last active December 22, 2015 05:59
Repro script for librato. You can remove the VCR stuff it that helps; I added it to log the requests and responses.
require 'librato/metrics'
require 'vcr'
VCR.configure do |vcr|
vcr.hook_into :faraday
vcr.cassette_library_dir = Dir.pwd
end
puts "Running on ruby version: #{RUBY_DESCRIPTION}"
puts "Using librato-metrics version: #{Librato::Metrics::VERSION}"
shared_examples_for "a search endpoint" do |queries_to_test|
queries_to_test.each do |query, expected_results|
context "for query: #{query}" do
let(:url) { build_url(resource, query) }
it 'is a valid endpoint' do
get url
expect(last_response.status).to eq 200
end
end
@myronmarston
myronmarston / rspec_matchers.md
Created June 29, 2013 04:20
Why I prefer RSpec's matchers to xUnit-style assertions

Why I prefer RSpec's matchers to xUnit Assertions

Matchers make the ordering of arguments intuitive

As others have pointed out, it's not intuitive what the order of arguments is for assert_equal(foo, bar). Which is expected? Which is actual? I had to look it up. Compare that to expect(bar).to eq(foo) -- which is expected and which is actual is immediately obvious. assert_equal is one of the simplest assertions. It gets worse when you start

class Foo
def long_string
"a" * 170
end
def long_string_with_space
long_string = long_string + " "
long_string
end
end
@myronmarston
myronmarston / spec_helper.rb
Created May 15, 2013 15:09
Mock with multiple frameworks based on metadata. In reply to https://twitter.com/_solnic_/status/334682956771778560
# This is totally untested, but something like this should work...
module MultipleMockFrameworksAdapters
extend Forwardable
def_delegators :mock_framework_for_current_example,
# these are the 3 methods your adapter needs
:setup_mocks_for_rspec,
:verify_mocks_for_rspec,
:teardown_mocks_for_rspec
def mock_framework_for_current_example
@myronmarston
myronmarston / irb_session
Created April 17, 2013 04:37
Why does `public_class_method` in an anonymous module make the method public on `Object`?
➜ irb
1.9.3p327 :001 > Object.define_method
NoMethodError: private method `define_method' called for Object:Class
from (irb):1
from /Users/myron/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :002 > m = Module.new do
1.9.3p327 :003 > public_class_method :define_method
1.9.3p327 :004?> end
=> #<Module:0x007fd4c493d8e8>
1.9.3p327 :005 > Object.define_method
@myronmarston
myronmarston / stack_overflow_debugger.rb
Created March 30, 2013 21:20
Debug SystemStackError with this utility file.
max_stack_frames = 500
TooManyStackFrames = Class.new(StandardError)
set_trace_func proc { |event, file, line, id, binding, classname|
if event == "call" && caller.size >= max_stack_frames
raise TooManyStackFrames, "Stack has exceeded #{max_stack_frames} frames"
end
}
require 'spec_helper'
describe MyClass do
it 'does something', :wip do
end
it 'does something else' do
end
end
@myronmarston
myronmarston / crazy_ruby_bug.md
Last active December 14, 2015 17:38
The craziest ruby bug I've ever seen. I don't understand it. At all.

I've discovered a crazy bug that's really confusing me. I'm curious to hear if anyone can explain it.

Here's some code in foo.rb:

class Superclass
  unless ENV['NORMAL_METHOD_DEF']
    define_method :regex do
      /^(\d)$/
    end