Skip to content

Instantly share code, notes, and snippets.

View tdg5's full-sized avatar

Danny Guinther tdg5

View GitHub Profile
@tdg5
tdg5 / mocha_calls_through.rb
Last active January 11, 2016 15:43
Mocha Expectation#calls_through
module Concerns
module Mocha
module CallsThrough
def calls_through
method_name = @method_matcher.expected_method_name
@mock.instance_variable_get(:@receiver)
@original_method = @mock.instance_variable_get(:@mockery).stubba.stubba_methods.find do |mockery|
mockery.method == method_name
end.instance_variable_get(:@original_method)
self
@tdg5
tdg5 / fingerprintedTemplates.js
Created January 21, 2014 11:37
An ERB precompiled AngularJS service for Rails allowing easy access to HTML templates fingerprinted by the Asset Pipeline.
<%
# Generate lookup Hash of fingerprinted filenames for html templates
# Leave regexp open ended to include preprocessed templates (.erb, etc.)
template_regexp = /(?:\/assets\/templates\/)(.*\.html)/
template_files = Rails.application.assets.each_file.to_a.join("\n").scan(template_regexp).flatten
templates = Hash[template_files.map {|file| [file, asset_url(file)] }]
%>
var fingerprintedTemplates = angular.module('services.fingerprintedTemplates', []);
fingerprintedTemplates.factory('fingerprintedTemplates', [
@tdg5
tdg5 / gist:7891302
Created December 10, 2013 14:21
beanstalkd peek-oldest command
diff --git a/dat.h b/dat.h
index 2570e76..ac6f6e7 100644
--- a/dat.h
+++ b/dat.h
@@ -204,6 +204,9 @@ void job_free(job j);
/* Lookup a job by job ID */
job job_find(uint64 job_id);
+/* Lookup oldest job */
+job job_oldest();
@tdg5
tdg5 / README.md
Last active November 2, 2015 13:06
Patch to install Ruby with tail call optimization enabled by default

Installation with RVM:

  $ rvm install 2.2.3 --patch https://gist.githubusercontent.com/tdg5/0fba32242ccc39e71577/raw/b0d869e006761c74dac4ed9dbb779a56b424b63c/ruby_tco.patch
@tdg5
tdg5 / cassandra_cleanup.java
Last active October 30, 2015 19:48
Various bits of the cassandra cleanup process code
/*
https://github.com/apache/cassandra/blob/cassandra-2.0/src/java/org/apache/cassandra/db/compaction/CompactionManager.java#L523
*/
private void doCleanupCompaction(final ColumnFamilyStore cfs, Collection<SSTableReader> sstables, CounterId.OneShotRenewer renewer) throws IOException
{
assert !cfs.isIndex();
Keyspace keyspace = cfs.keyspace;
Collection<Range<Token>> ranges = StorageService.instance.getLocalRanges(keyspace.getName());
if (ranges.isEmpty())
{
@tdg5
tdg5 / tail_positions.rb
Created October 12, 2015 20:24
Script to explore in which situations tail call elimination can occur in Ruby
require "tco_method"
# Wrap method definitons in a lambda to facilitate recompiling with tail call
# optimization enabled.
tail_calls = lambda do |this|
def start_tail_call_chain
implicit_tail_call
end
def implicit_tail_call
@tdg5
tdg5 / unary_ampersand_keeping_refs.rb
Last active October 7, 2015 14:19
Example seeming to demonstrate that unary ampersand operator holds on to obj references in Ruby 2.1.x
# Create some special classes to facilitate tracking allocated objects.
class TrackedArray < Array; end
class TrackedString < String; end
STRANG = "a" * 5000
class ClingyObjects
def generate(should_cling = false)
strs = TrackedArray.new
30000.times { strs << TrackedString.new(STRANG) }
@tdg5
tdg5 / gc_friendly_tco.rb
Created October 7, 2015 12:55
Example demonstrating that tail-call optimization in MRI Ruby allows for earlier garbage collection of unreachable objects.
# Create some special classes to facilitate tracking allocated objects.
class TrackedArray < Array; end
class TrackedString < String; end
STRANG = "a" * 5000
# Other than a few extra escapes, the code below can be understood more or less
# as is. Any weirdness is to facilitate interpretting with/without tail-call
# optimization in a DRY manner.
KLASS = <<-CODE
class GcFriendlyTco
@tdg5
tdg5 / better_oo_through_tco.rb
Last active September 30, 2015 12:34
Better Object-Oriented Design Through... Tail-Call Optimization?
# Abstract UnionList base class
class AbstractUnionList
private :initialize
def length
how_many?(0)
end
def how_many?(counter)
raise NotImplementedError
@tdg5
tdg5 / pry_hooks.rb
Created June 9, 2015 23:16
Pry Hooks!
%w[
after_read
before_eval
after_eval
when_started
before_session
after_session
].each do |hook|
Pry.config.hooks.add_hook(hook.to_sym, "my_#{hook}_hook") do |_, _, _|
puts "hello from #{hook}"