Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@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
Created October 15, 2025 15:58
An example showing how to convert a Proc into a lambda with Fiddle
require 'fiddle'
module ProcToLambda
WORD = Fiddle::SIZEOF_VOIDP
RPROC = 4 * WORD
OFFSET = 32
VALUE = 2
refine Proc do
def to_lambda
@havenwood
havenwood / papercraft.rb
Created October 14, 2025 18:51
My suggestion for a lambda-like strict arity alternative to a block
module Papercraft
class Block
def initialize(&) = singleton_class.define_method(:call, &)
def apply(...) = self.class.new { call(...) }
class HTML < Block
def render(...) = call(...)
def to_html = render.to_s
end
end
@havenwood
havenwood / options.rb
Created October 8, 2025 15:15
An example of `Ruby::Box` isolating globals (due to be released on Christmas)
class Options < Ruby::Box
def [](name) = instance_eval { eval "$#{name.to_s.delete_prefix('$')}" }
end
options = Options.new
options.instance_eval do
require 'optparse'
class Option
def []=(key, value)
@havenwood
havenwood / global_variable.rb
Created September 3, 2025 20:15
Set & get Ruby global variables using Fiddle
require 'fiddle'
require 'fiddle/import'
module GlobalVariable
extend Fiddle::Importer
dlload Fiddle::Handle::DEFAULT
extern 'void rb_gv_set(const char*, unsigned long)'
extern 'unsigned long rb_gv_get(const char*)'
end
@havenwood
havenwood / command.rb
Created August 2, 2025 19:20
Example io-stream for #ruby IRC
require 'io/stream'
require 'open3'
module Command
Result = Data.define(:out, :err, :status)
def self.run(*, **)
Open3.popen3(*, **) do |stdin, stdout, stderr, thread|
stdin.close
@havenwood
havenwood / document.rb
Last active June 30, 2025 17:27
Mixin example for #ruby IRC
module Document
def self.included(base)
base.class_eval do
def self.name = "#{super}ument"
end
end
module_function
def print = :printed
@havenwood
havenwood / trad_sleep.rb
Created May 8, 2025 23:12
Just a sleep-alarm based thingy for an IRC example
signal = 'ALRM'
status = Signal.list.fetch(signal) + 2**7
pid = fork do
trap signal do
warn 'Alarm!'
exit status
end
sleep
@havenwood
havenwood / zipped_io.rb
Created April 27, 2025 21:12
Just a fun example for Ruby IRC playing with IO.copy_stream
require 'stringio'
require 'zlib'
io = StringIO.new('Zip me...')
zipped_io = StringIO.new('', 'a+b')
writer = Zlib::GzipWriter.new(zipped_io)
IO.copy_stream(io, writer)
writer.finish
zipped_io.rewind
@havenwood
havenwood / sync_pipe.rb
Created March 23, 2025 04:18
An example refinement method like `IO.pipe`'s block form but with no buffering.
class IO
module UnbufferedPipe
refine IO.superclass do
def unbuffered_pipe(*, **)
IO.pipe(*, **) do |read_io, write_io|
read_io.sync = true
write_io.sync = true
yield read_io, write_io
end