Skip to content

Instantly share code, notes, and snippets.

View soulcutter's full-sized avatar

Bradley Schaefer soulcutter

View GitHub Profile
@soulcutter
soulcutter / rubyprof.rake
Created April 14, 2012 06:17
Ruby profiling rake task
require 'ruby-prof'
require 'ruby-prof/task'
RubyProf::ProfileTask.new(:integration) do |t|
t.test_files = FileList['test/integration/**/patients_test.rb']
t.output_dir = File.join(Rails.root, 'tmp')
t.printer = :graph_html
t.min_percent = 10
end
require "delegate"
Foo = Class.new(SimpleDelegator)
Bar = Struct.new(:foo)
Foo.new(1) == Foo.new(1) # => true
Bar.new(1) == Bar.new(1) # => true
Foo.new(Bar.new(1)) == Foo.new(Bar.new(1)) # => false
@soulcutter
soulcutter / bounded_hash.rb
Created October 3, 2016 21:47
A bounded hash that expires keys using LRU (roughly)
# frozen_string_literal: true
require "delegate"
require "set"
# Hash that can only hold a certain number of keys, keys expire on a LRU basis
class BoundedHash < DelegateClass(Hash)
NO_OP = proc {}
@soulcutter
soulcutter / be_uniq.rb
Created September 6, 2016 18:24
RSpec basic flat array uniqueness matcher
RSpec::Matchers.define :be_uniq do
match do |actual|
values_match? actual.uniq, actual
end
failure_message do |actual|
diff = actual.dup
actual.uniq.each { |value| diff.delete_at diff.index(value) }
diff = diff.uniq
"expected that #{actual.inspect} to be uniq, but found the following repeated elements: #{diff.inspect}"
@soulcutter
soulcutter / fluent.rb
Last active March 4, 2016 17:27
Fluent builder interface
module Fluent
def fluent_accessor(*attr_names)
attr_names.each do |attr_name|
define_method attr_name do |*val|
return instance_variable_get("@#{attr_name}") if val.empty?
raise ArgumentError, "Expected 0..1 arguments, got #{val.size}" if val.size > 1
value = val.first
value = yield(value) if block_given?
@soulcutter
soulcutter / xor.txt
Created February 8, 2016 23:35
What am I xorring?
ternary = foo ? !bar : bar
xor = !!(foo ^ bar)
neq = foo != bar
foo | bar | xor | ternary | neq
-------------------------------------
true | true | false | false | false
false | false | false | false | false
true | false | true | true | true
false | true | true | true | true
@soulcutter
soulcutter / Gruntfile.js
Created September 23, 2013 23:17
Grunt / Angular setup based originally on Yeoman
'use strict';
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
grunt.loadNpmTasks('grunt-contrib-watch');
// configurable paths
var config = {
@soulcutter
soulcutter / barator.rb
Last active December 20, 2015 07:19
instance_method owners don't always respond to method_defined?
module Barator
def foo(*args)
"#{super(*args)}bar"
end
end
class Foo
prepend Barator
def foo
@soulcutter
soulcutter / Backtrace
Created July 26, 2013 19:00
Weird stubbing error
Order rejecting an order sends reject email when status is updated
Failure/Error: Order.new.stub(:send_reject_email)
NoMethodError:
undefined method `method_defined?' for #<Order not initialized>
# /Users/matt/.rvm/gems/ruby-2.0.0-p247@uncommon-flow/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'
# /Users/matt/.rvm/gems/ruby-2.0.0-p247@uncommon-flow/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:128:in `method_missing'
# /Users/matt/.rvm/gems/ruby-2.0.0-p247@uncommon-flow/gems/rspec-mocks-2.14.1/lib/rspec/mocks/instance_method_stasher.rb:34:in `method_defined_on_klass?'
# /Users/matt/.rvm/gems/ruby-2.0.0-p247@uncommon-flow/gems/rspec-mocks-2.14.1/lib/rspec/mocks/instance_method_stasher.rb:55:in `method_owned_by_klass?'
# /Users/matt/.rvm/gems/ruby-2.0.0-p247@uncommon-flow/gems/rspec-mocks-2.14.1/lib/rspec/mocks/instance_method_stasher.rb:29:in `method_defined_directly_on_klass?'
# /Users/matt/.rvm/gems/ruby-2.0
@soulcutter
soulcutter / gist:5936232
Last active December 19, 2015 09:49 — forked from cstump/gist:5936186
require 'active_support'
module TestConcern
extend ActiveSupport::Concern
module ClassMethods
def test_class_method
puts "I gots class!"
end
end