Skip to content

Instantly share code, notes, and snippets.


Shannon Skipper havenwood

Block or report user

Report or block havenwood

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
havenwood / multiple_instances_of_state.rb
Last active Feb 21, 2020
Examples of a module, class and singleton class in Ruby
View multiple_instances_of_state.rb
class MultipleInstancesOfState
attr_accessor :state
def initialize(state:)
@state = state
def foo
havenwood /
Created Feb 18, 2020
Isolates in Ruby (an early look at ko1's work)
git clone --depth 1 --single-branch --branch guild ruby-isolates-src
cd ruby-isolates-src
./configure --prefix=$HOME/.rubies/ruby-guilds # chruby
# ./configure --prefix=$HOME/.rvm/rubies/ruby-guilds # RVM
# ./configure --prefix=$HOME/.rbenv/versions/3.0.0/ruby-guilds # rbenv
make -j2
make install
havenwood / fix_strptime.rb
Last active Jan 25, 2020
A refinement to fix the Time::strptime lack of %U/%u/%W/%w/%V/%g/%G support pre-2.7 — see
View fix_strptime.rb
require 'time'
module FixStrptime
refine Time.singleton_class do
def strptime(date, format,
d = Date._strptime(date, format)
raise ArgumentError, "invalid date or strptime format - `#{date}' `#{format}'" unless d
if seconds = d[:seconds]
if sec_fraction = d[:sec_fraction]
usec = sec_fraction * 1000000
havenwood / benchmark.rb
Created Jan 23, 2020
Benchmark of various solutions to a question on #ruby IRC
View benchmark.rb
# frozen_string_literal: true
require 'benchmark/ips'
Benchmark.ips do |ips|
words = [*'a'..'all'].freeze
separator = '-''map & join') { { |word| separator + word }.join }'join & prepend') { words.join(separator).prepend(separator) }
havenwood / fiberator.rb
Created Jan 20, 2020
My WIP implementation of Enumerator backed by a Fiber (like the actually are) but in pure Ruby
View fiberator.rb
# frozen_string_literal: true
require 'delegate'
class Fiberator < DelegateClass Fiber
class Rewind < StandardError
private_constant :NOTHING
havenwood / tally.rb
Created Jan 15, 2020
A look at how to tally a collection in Ruby, from Ruby 1.9 to 2.7.
View tally.rb
# Ruby 1.9
Hash[collection.group_by { |n| n }.map { |k, v| [k, v.size] }]
# Ruby 2.0 Array#to_h
collection.group_by { |n| n }.map { |k, v| [k, v.size] }.to_h
# Ruby 2.2 Object#itself
collection.group_by(&:itself).map { |k, v| [k, v.size] }.to_h
# Ruby 2.4 Hash#transform_values
havenwood / red_inspect.rb
Created Jan 9, 2020
Not advocating doing this—just think it's interesting
View red_inspect.rb
module RedInspect
REFINED = [NilClass, Array, Array.singleton_class].freeze
def inspect
self.then do |oneself|
REFINED.each { refine(_1) { include oneself } }
havenwood / size_squared.rb
Last active Jan 8, 2020
My refinement suggestion for a #ruby IRC question about monkey patching
View size_squared.rb
module SizeSquared
[String, Enumerable].each do |this_class|
refine this_class do
def size_squared
alias length_squared size_squared
havenwood / jekyll_plugins.rb
Last active Jan 3, 2020
My solution to a #ruby IRC question
View jekyll_plugins.rb
# frozen_string_literal: true
fetcher = Gem::SpecFetcher.fetcher
matches = fetcher.detect do |spec| 'jekyll-'
plugins =
havenwood / now_and_every.rb
Last active Dec 24, 2019
An example showing time now slipping, as long as job takes less time than interval
View now_and_every.rb
require 'async'
require 'async/notification'
def do_a_thing_lasting(duration)
puts 'hi'
sleep duration
puts 'bye'
def now_and_every(interval)
You can’t perform that action at this time.