Skip to content

Instantly share code, notes, and snippets.

Bradley Schaefer soulcutter

Block or report user

Report or block soulcutter

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
soulcutter / test.rb
Created Mar 8, 2019
create_or_find_by sequence exhaustion
View test.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source ""
git_source(:github) { |repo| "{repo}.git" }
gem "rails", github: "rails/rails"
soulcutter / speed_limit.rb
Created Apr 8, 2018
A simple way to throttle enumeration
View speed_limit.rb
class SpeedLimit
def initialize(enumerator)
@enumerator = enumerator.to_enum
def at(count, per:)
increment_seconds = case per
when :second then 1.0 / count.to_f
when :minute then 60.0 / count.to_f
when :hour then 3600.0 / count.to_f
soulcutter / tapper.rb
Created Mar 9, 2018
Tap vs Local Variables
View tapper.rb
def level_descriptions=(input)
write_attribute(:levels_description, input).tap do |_result|
flush_cache :levels_description
# same number of lines, one less method call, one less block, but pretty much the same as
# far as I'm concerned.
def level_descriptions=(input)
result = write_attribute(:levels_description, input)
View memoize_example.rb
module Memo
def memoize(method_name)
original_method = instance_method(method_name)
method_cache = { |h, k| h[k] = {} }
define_method(method_name) do |*args, &block|
if method_cache[self].key?([args, block])
method_cache[self][[args, block]]
method_cache[self][[args, block]] = original_method.bind(self).call(*args, &block)
soulcutter / instrumentor.rb
Created Nov 14, 2017
Instrumentation for methods
View instrumentor.rb
module Instrumenter
class Instrumentation < Module
def initialize(method, label)
@method = method
@label = label
define_method(method) do |*args|
start_time =
super(*args).tap do
end_time =
soulcutter / urinal_game.rb
Last active Nov 5, 2017
Example of what object syntax could look like
View urinal_game.rb { |urinal| }
Urinal =, :location) { def adjacent_urinals; location.adjacent_urinals; end }
View delegates.rb
require "delegate"
Foo =
Bar = == # => true == # => true == # => false
soulcutter / bounded_hash.rb
Created Oct 3, 2016
A bounded hash that expires keys using LRU (roughly)
View bounded_hash.rb
# 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 / be_uniq.rb
Created Sep 6, 2016
RSpec basic flat array uniqueness matcher
View be_uniq.rb
RSpec::Matchers.define :be_uniq do
match do |actual|
values_match? actual.uniq, actual
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 / fluent.rb
Last active Mar 4, 2016
Fluent builder interface
View fluent.rb
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?
You can’t perform that action at this time.