Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 19:17 (UTC -08:00)
View GitHub Profile
@havenwood
havenwood / app.rb
Created January 8, 2026 19:58
Example showing running Falcon locally from a single file (IRC question about ditching config.ru)
#!/usr/bin/env -S falcon-host
# frozen_string_literal: true
require "falcon/environment/rack"
require "roda"
class App < Roda
route do |r|
r.root { "wombat" }
end
@havenwood
havenwood / options.rb
Created January 6, 2026 22:15
An example just to demonstrate how the new `Ruby::Box` sandboxes stuff including globals
require 'optparse'
class Options < Ruby::Box
class Into
class BadKeyError < ArgumentError
def initialize(key) = super("Bad key: #{key.inspect}")
end
def []=(key, value)
global = "$#{key.to_s.delete_prefix('$').tr('-', '_')}"
@havenwood
havenwood / byte-size.js
Created December 31, 2025 19:50
An example custom HTML element that shows formatted byte sizes
class ByteSize extends HTMLElement {
static observedAttributes = ["si"];
static #binaryUnits = ["B", "KiB", "MiB", "GiB", "TiB"];
static #siUnits = ["B", "KB", "MB", "GB", "TB"];
#initialized = false;
#bytes = 0;
get si() {
return this.hasAttribute("si");
@havenwood
havenwood / options.rb
Created December 28, 2025 02:19
An example showing OptionParser backed by Data
require 'delegate'
require 'optparse'
class Options < SimpleDelegator
def initialize(argv = ARGV, &)
parser = OptionParser.new(&)
members = members_from parser
values = values_from parser, argv
data = data_from members, values
@havenwood
havenwood / stream_zipped_tsv.rb
Created December 11, 2025 12:55
An example showing streaming a zipped tsv via a pipe
require 'async'
require 'async/http/internet/instance'
require 'csv'
require 'zlib'
def stream_zipped_tsv(url, &)
Sync do
response = Async::HTTP::Internet.get(url)
unless response.success?
@havenwood
havenwood / curtain.rb
Created November 27, 2025 01:33
at_exit with priorities
module Curtain
@hooks = []
@order = 0
@mutex = Mutex.new
class << self
attr_reader :order
def register(group:, priority: 0, &block)
@mutex.synchronize do
@havenwood
havenwood / ruby_vm-shape.rb
Created November 15, 2025 19:38
A nicer interface to look at the Shape of a Ruby Object
require 'json'
require 'objspace'
class RubyVM
module Shape
def self.id(obj)
json = ObjectSpace.dump(obj, output: :string)
JSON.parse(json).fetch('shape_id')
end
@havenwood
havenwood / tags.rb
Created November 15, 2025 19:34
An example of ObjectSpace::WeakKeyMap, which was introduced in Ruby 3.3
require 'delegate'
class Tags < SimpleDelegator
def initialize = super(ObjectSpace::WeakKeyMap.new)
def tagged?(key) = key?(key)
end
TAGS = Tags.new
lamb = -> {}
@havenwood
havenwood / literalparser.rb
Created October 29, 2025 09:40 — forked from apeiros/literalparser.rb
Parse literals to ruby objects
require 'strscan'
require 'bigdecimal'
# This is copied and slightly refactored from BareTest::TabularData
#
# Example
# LiteralParser.parse("nil") # => nil
# LiteralParser.parse(":foo") # => :foo
@havenwood
havenwood / proc_to_lambda.rb
Last active November 5, 2025 04:50
An example showing how to convert a Proc into a lambda and vice versa with Fiddle
require 'fiddle'
module Procify
WORD = Fiddle::SIZEOF_VOIDP
RPROC_OFFSET = 4 * WORD
PROC_FLAG_OFFSET = 32
PROC_PACKED = [0].pack('Q').freeze
LAMBDA_PACKED = [2].pack('Q').freeze
refine Proc do