Skip to content

Instantly share code, notes, and snippets.


Shannon Skipper havenwood

View GitHub Profile
havenwood / example.rb
Created Aug 31, 2022
Rust-inspired `struct` and `impl` in Ruby for fun
View example.rb
require_relative 'kernel'
struct :Point do
x: 0,
y: 0,
z: 0
havenwood / arithmetic_sequence.rb
Created Aug 21, 2022
An example of implementing a JSON addition to support Enumerator::ArithmeticSequence
View arithmetic_sequence.rb
# frozen_string_literal: true
require 'json'
class Enumerator
class ArithmeticSequence
def to_json(*args)
JSON.create_id =>,
'begin' => self.begin,
havenwood / atomic_move.rb
Created Aug 12, 2022
Example of atomic move for #ruby IRC
View atomic_move.rb
require 'fileutils'
require 'tempfile'
def write_atomically(content, temp_name: 'temp.txt', atomic_path: 'atomic.txt')
temp = temp_name
temp.write content
temp.close temp.path, atomic_path
havenwood / each.rb
Created Jul 5, 2022
An example of why we use `each` rather than `for` and what semicolon block args do for #ruby IRC
View each.rb
foo = :untouched
bar = :untouched
[:touched].each do |foo|
bar = :touched
foo #=> :untouched
bar #=> :touched
havenwood / class_mixin.rb
Created Jul 3, 2022
My example for Ruby #irc showing how you can contort and mixin class methods.
View class_mixin.rb
require 'digest/md5'
class_name = Digest::MD5
module class_name::Mixin
class_name::Mixin.class_eval do
class_name.public_methods(false).each do |method_name|
define_method method_name, class_name.public_method(method_name).to_proc
havenwood / skynet.rb
Created May 3, 2022
A port of the Crystal "skynet" code to Ruby, see
View skynet.rb
require 'async'
require 'async/queue'
def skynet(c, num, size, div)
if size == 1
c << num
rc =
sum = 0
div.times do |i|
havenwood / example.rb
Last active Apr 29, 2022
Kaprekar's Constant in Ruby
View example.rb
require_relative 'kaprekar'
include Kaprekar
p kaprekar(four_digit_number: 9000).first(10)
#>> [9000, 8991, 8082, 8532, 6174, 6174, 6174, 6174, 6174, 6174]
p kaprekar(four_digit_number: 9000).take_while { _1 != Kaprekar::CONSTANT }
#>> [9000, 8991, 8082, 8532]
View Gemfile
# frozen_string_literal: true
source ''
gem 'rack', github: 'rack/rack'
gem 'puma', github: 'puma/puma'
havenwood / store.rb
Created Feb 8, 2022
Handling nested PStore transactions (IRC example)
View store.rb
require 'pstore'
class Store
def initialize(relative_path: '.data.pstore')
@store =, true)
@store.ultra_safe = true
@store.transaction(true) { }
@in_transaction = false
View async.rb
require 'async'
class Enumerator
class Async
module Refinement
refine Enumerable do
def async