Skip to content

Instantly share code, notes, and snippets.

View matthewd's full-sized avatar

Matthew Draper matthewd

View GitHub Profile
@matthewd
matthewd / db-switch.rb
Created April 1, 2021 05:50
bin/db-switch
#!/usr/bin/env ruby
require(Dir.pwd + "/config/environment")
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
ActiveRecord::Base.establish_connection(db_config.config)
context = ActiveRecord::Base.connection.migration_context
missing_migrations = []
@matthewd
matthewd / diff-configs.sh
Created September 30, 2011 19:48
Diff /etc files against the versions supplied in Debian packages
#!/bin/bash
# This script will make a best-effort attempt at showing modifications
# to package-provided config files on a Debian system.
#
# It's subject to some pretty significant limitations: most notably,
# there's no way to identify all such config files. We approximate the
# answer by looking first at dpkg-managed conffiles, and then hoping
# that most of the time, if maintainer scripts are managing files
# themselves, they're using ucf. So, DO NOT TRUST THIS SCRIPT to find
@matthewd
matthewd / fallout.rb
Last active April 4, 2019 00:35 — forked from tenderlove/fallout.rb
hack fallout terminals
##
# Program to help you hack terminals in Fallout
#
# Usage:
#
# Run this program with a '/' separated list of possible words, and
# (once you've made a guess) another '/' separated list of words and
# scores where the word and score are separated by a ':'
#
# In this example, the words shown in my terminal were:
class Range
def overlap(other)
[self.begin, other.begin].max .. [self.end, other.end].min
end
end
def musical_era_alive_in(start_life, end_life)
[
['Medieval', 476..1400],
['Renaissance', 1400..1600],
@matthewd
matthewd / -
Created February 18, 2016 21:22
commit 604fd2cb227d92bed1b738e71feb4ff5360f4491
Author: Matthew Draper <matthew@trebex.net>
Date: Fri Feb 19 07:50:42 2016 +1030
Use a semaphore to signal message availability
The Event hack was too much of a hack: on actually thinking about it,
there's a rather obvious race.
diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb
@matthewd
matthewd / -
Created February 18, 2016 21:20
diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb
index d30c381..2662921 100644
--- a/actioncable/test/client_test.rb
+++ b/actioncable/test/client_test.rb
@@ -54,7 +54,7 @@ def initialize(port)
@ws = Faye::WebSocket::Client.new("ws://127.0.0.1:#{port}/")
@messages = Queue.new
@closed = Concurrent::Event.new
- @has_messages = Concurrent::Event.new
+ @has_messages = Concurrent::Semaphore.new(0)
@matthewd
matthewd / -
Created January 22, 2016 21:10
diff --git a/lib/sprockets/manifest.rb b/lib/sprockets/manifest.rb
index e514ebb..9797575 100644
--- a/lib/sprockets/manifest.rb
+++ b/lib/sprockets/manifest.rb
@@ -165,6 +165,8 @@ def compile(*args)
concurrent_compressors = []
concurrent_writers = []
+ pool = Concurrent::FixedThreadPool.new(5)
+
@matthewd
matthewd / gist:6541214
Last active December 22, 2015 22:39
Calling an association through another, without hm:t?
class Order < AR::Base
has_many :widgets
has_many :parts, through: :widget
end
class Widget < AR::Base
has_many :parts
end
class Part < AR::Base
@matthewd
matthewd / common-setup.sh
Last active December 17, 2015 14:29
.common/setup
#!/bin/bash
COMMON=.common
if [ ! -d "$HOME/$COMMON" ]; then
if ! which git >/dev/null 2>&1; then
if which apt-get >/dev/null 2>&1 && which sudo >/dev/null 2>&1; then
if ! sudo apt-get install git-core; then
echo "Failed to install git; can't check out files" >&2
exit 1
@matthewd
matthewd / gist:1697002
Created January 29, 2012 03:33
Check for unused aliases
check_an_alias() {
alias_name="$1"
typed_cmd=(${(zQ)2})
alias_result=(${(zQ)3})
while [[ "${alias_result[1]}" = "noglob" ]] || [[ "${alias_result[1]}" = "nocorrect" ]]; do
shift alias_result
done
if [[ "${#alias_result}" -gt "${#typed_cmd}" ]]; then