Skip to content

Instantly share code, notes, and snippets.

View yaauie's full-sized avatar

Ry Biesemeyer yaauie

View GitHub Profile
# Get reasonable CLI confirmation before continuing.
#
# @param text [String] - the descriptive text
# @param options [Hash{Symbol=>Object}]
# @option options [Boolean] :default - the default action (false)
# @option options [#puts] :out - the IO on which to puts output ($stderr)
# @option options [#readline] :in - the IO on which to readline input ($stdin)
# @option options [String] :prompt - the prompt string ("ok?")
#
# @example
@yaauie
yaauie / sleepy.rb
Created May 21, 2014 07:05
The following demonstrates that Thread#wakeup can be used in a signal trap to wake up the thread that had control when the signal was raised. I have verified same behaviour in MRI (1.8.7, 1.9.3, 2.0.1, 2.1.1), JRuby (1.7.4, 1.9 mode), Rubinius (2.2.1), but testing signals is hard and I'm unsure how to verify this programatically.
$ ruby ~/Desktop/sleepy.rb
signals registered.
^CCAUGHT INT!
4
# encoding: utf-8
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Ryan Biesemeyer <wtf@yaauie.com>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
@yaauie
yaauie / yaauie-semaphore.rb
Last active August 29, 2015 14:05
A quick spike on a one-size-fits-all semaphore in ruby; ordering is *not* guaranteed when write locks are released.
require 'thread' # Mutex
require 'monitor' # MonitorMixin::ConditionVariable
# A semaphore that supports Read Locks, Write Locks, and Sized Semaphores
class Yaauie::Semaphore
INFINITY = Float::INFINITY
# @param options [Hash{Symbol=>Object}]
# @option options [Integer, INFINITY] :readers_max (INFINITY)
def initialize(options = {})
def time_thread_contention(options = {})
thread_count = options.fetch(:threads, 10)
iterations = options.fetch(:iterations, 10)
start_time = Time.now
iterations.times.each_slice(thread_count).to_a.transpose.map do |idxs|
Thread.new do
idxs.each do |idx|
yield
Thread.pass if idx.odd?
@yaauie
yaauie / resque-scheduler-find-delayed-job.rb
Last active August 29, 2015 14:05
Scan through all Resque Scheduler delayed jobs and emit the timestamps and payload of all matching jobs.
# @param search [String, Regexp]
# @yieldparam time [Time]
# @yieldparam payload [String]
# @yieldreurn [void]
# @return [void]
def find_delayed_jobs(search)
return enum_for(__method__, search) unless block_given?
min = 0
max = '+inf'
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# encoding: utf-8
require 'openssl'
class EasyKrypt
class Error < RuntimeError
def initialize(*args)
@cause = $!
end
attr_reader :cause
@yaauie
yaauie / gi
Created November 21, 2014 19:14
Lately I've been fat-fingering git commands, injecting a space between `gi` and `t` and failing to inject a space between that `t` and the command; this simple script fixes that.
#!/usr/bin/env ruby
# gi - a dynamic alias for poorly-typed git commands
# handle a command `gi tdiff --cached`,
# transparently as `git diff --cached`.
exit(127) if ARGV.empty?
exit(127) unless ARGV.first.start_with?('t')
argv = ARGV.dup
argv.unshift(argv.shift[1..-1])
@yaauie
yaauie / redis-expiry-helper.md
Created November 25, 2014 02:16
Redis Expiry Helper

Sometimes your redis needs a little help keeping up with expiries.

First, it's helpful to know how expiries are handled in redis internals:

Redis keys are expired in two ways: a passive way, and an active way.

A key is actively expired simply when some client tries to access it, and the key is found to be timed out.