Skip to content

Instantly share code, notes, and snippets.

View dbenhur's full-sized avatar

Devin Ben-Hur dbenhur

View GitHub Profile
@dbenhur
dbenhur / subset_bm.rb
Created July 5, 2012 22:19
StackOverflow - Ruby - Optimize the comparison of two arrays with duplicates
#!/usr/bin/env ruby
# benchmarks for http://stackoverflow.com/questions/11349544/ruby-optimize-the-comparison-of-two-arrays-with-duplicates/11352055#comment14951595_11352055
require 'rubygems'
require 'multiset'
require 'benchmark'
small_b = "cheddaar".split(//)
small_a = "cheddar".split(//)
@dbenhur
dbenhur / thread_safe_expectation_spec.rb
Created April 12, 2012 00:41
RSpec #should_recieve can't count correctly with threads
require 'rspec'
T = 10000
N = 200
class ConcurrentSender
def initialize(collaborator,message,threads,sends)
@collaborator,@message,@threads,@sends = collaborator,message,threads,sends
@tasks = []
end
@dbenhur
dbenhur / match_method.rb
Created December 8, 2011 01:03 — forked from avdi/match_method.rb
Defining method_missing and respond_to? in one fell swoop
# Do you ever define #method_missing and forget #respond_to? I sure
# do. It would be nice if we could do them both at the same time.
module MatchMethodMacros
def match_method(matcher, &method_body)
mod = Module.new do
define_method(:method_missing) do |method_name, *args, &blk|
if matcher === method_name.to_s
instance_exec(method_name, *args, blk, &method_body)
else
def transform_hash(original, options={}, &block)
options[:safe_descent] ||= {}
new_hash = {}
options[:safe_descent][original.object_id] = new_hash
original.inject(new_hash) { |result, (key,value)|
if (options[:deep] && Hash === value)
value = options[:safe_descent].fetch( value.object_id ) {
transform_hash(value, options, &block)
}
end