Skip to content

Instantly share code, notes, and snippets.

@stevecj
stevecj / sleep_request.rb
Last active Dec 11, 2015
Cleanly stoppable beaneater/beanstalkd agent. Waits for currently executing task to complete before stopping for INT (Ctrl+c) or TERM (kill) signals or receipt of an enqueued stop job.
View sleep_request.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require 'beaneater'
beanstalk = Beaneater::Pool.new(['localhost:11300'])
sleep_tube = beanstalk.tubes['sleeper-take-a-nap']
sleep_tube.put '-'
@stevecj
stevecj / defines_interface.rb
Created Oct 11, 2012
Interfaces for Ruby
View defines_interface.rb
# A module for inclusion in a class of objects that delegate to and
# provide restricted interface definitions for underlying "occurrence"
# objects.
#
# This is useful for purposes such as to enforce the same API for a
# unit under test as for mocks and stubs of the same unit used for
# testing other units.
#
# This is a proof of concept demonstration and is not well-tested,
# production-ready code.
@stevecj
stevecj / computes_recursively.rb
Last active Nov 27, 2015
Fake tail recursion in Ruby without relying on tail optimization support in the Ruby VM
View computes_recursively.rb
# This is a mixin module that adds support for tail-recursive
# style programming in Ruby without relying on any true tail
# recursion optimization in the Ruby virtual machine.
#
# Since tail recursion calls are actually deferred until after
# returning from the method that invoked #tail, recursions can
# be "nested" to an unlimited depth without overflowing the
# stack.
#
# The including module can define tail-recursive methods within
@stevecj
stevecj / ruby_nesting_unmuddling.rb
Created Mar 16, 2012
Understanding and unscrambling Ruby's weird module nesting behavior
View ruby_nesting_unmuddling.rb
# Get ourselves a clean, top-level binding.
def main_binding
binding
end
module ModuleUtils
module ModuleMethods ; end
self.extend ModuleMethods
module ModuleMethods
@stevecj
stevecj / ruby_module_nesting.rb
Created Mar 13, 2012
Ruby module nesting and scope of constant names
View ruby_module_nesting.rb
# == Module name assignment ==
# A Ruby module's name is determined when it first becomes
# assigned as the value of a top-level constant or of a
# constant in a module that has received a name.
# The module's name represents the hierarchy of containership,
# and is not dependent upon the execution path of the code
# that defined the module (as opposed to the module nesting
# for defined methods -- see below).
p [self, self.class, Module.nesting] # => [main, Object, []]
@stevecj
stevecj / enhanced_attributable.rb
Last active Sep 30, 2015
Ruby attr_accessor enhancement
View enhanced_attributable.rb
# Include this module in a class to add enhanced attr_accessor
# functionality. With this module included, ...
#
# * You may provide a block to an attr_accessor call that accepts
# a base attribute name and returns a default value for each
# instance attribute.
# * Any attribute name consisting of a base name followed by a
# "?" suffix represents a boolean attribute.
#
# If the including class has its own #initialize method, that
@stevecj
stevecj / fieldset-alternatives.html
Created Jul 15, 2011
Spike of JS for forms with nested select-driven alternative fieldsets
View fieldset-alternatives.html
<!DOCTYPE html>
<html>
<head>
<style>
fieldset { display: block; }
.alternative { display:none; }
.alternative-selected { }
</style>
You can’t perform that action at this time.