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 / universe.rb
Created February 18, 2024 00:45
An example of how to create a custom `new` method for all Classes or just a particular Class
View universe.rb
class Class
def new(...)
warn 'Overriding Class#new'
instance = allocate
instance.send(:initialize, ...)
instance
end
end
@havenwood
havenwood / kernel.rb
Last active January 22, 2024 00:01
A spike implementing part of this nifty Ruby feature request: https://bugs.ruby-lang.org/issues/20196
View kernel.rb
# frozen_string_literal: true
module Kernel
def Binary(string, exception: true)
hex = string.b
hex.gsub!(/#[^#]*$\R*/, '')
hex.gsub!(/"[^"]*"/) do |quotes|
quotes[1..-2].unpack1('H*')
end
hex.delete!("\s\t")
@havenwood
havenwood / ring_buffer.rb
Created July 5, 2023 19:01
A ring buffer implementation example for Ruby Discord.
View ring_buffer.rb
# frozen_string_literal: true
class RingBuffer
Nothing = Data.define
EMPTY = Nothing.new
attr_reader :size
def initialize(capacity:)
@capacity = capacity
@havenwood
havenwood / color_registry.rb
Created July 3, 2023 17:35
Example of a Curses colors registry Singleton for IRC
View color_registry.rb
# frozen_string_literal: true
require 'curses'
require 'singleton'
module Isene
class ColorRegistry
include Singleton
attr_reader :colors
@havenwood
havenwood / yarb.rb
Created January 22, 2023 18:00
A quick example for #ruby IRC of yomikomu-like compiling Ruby to YARB IR bytecode, caching it to disk, then loading and evaling it later.
View yarb.rb
require 'zlib'
path = 'example.rb'
##
# Compile and cache Ruby binary to disk.
binary = RubyVM::InstructionSequence.compile_file(path).to_binary
binary_path = "#{path}.yarb.gz"
Zlib::GzipWriter.open(binary_path) do
_1.write(binary)
View wombat.rb
require 'forwardable'
class Wombat
NO_DEFAULT = Object.new
include Comparable
include Enumerable
extend Forwardable
def_delegators :@list, :[], :[]=, :fetch, :<<, :delete, :size, :<=>
@havenwood
havenwood / stream_closing.rb
Created January 19, 2023 16:47
Curious about line 66 and line 70 differences between Falcon and Puma handling of the stream.
View stream_closing.rb
# frozen_string_literal: true
require 'trenni/template'
##
# Streaming template
TEMPLATE = Trenni::Template.load(<<~'HTML')
<!DOCTYPE html>
<html lang="en">
<head>
@havenwood
havenwood / bendford.rb
Last active November 2, 2022 06:36
A quick implementation to generate numbers that roughly comply with Bendford's law.
View bendford.rb
module Bendford
VARIED_DIGITS = 5
PLACES = [
[0, *(1..9).map { Math.log10(1.fdiv(_1) + 1) }],
*(2..VARIED_DIGITS).map do |place|
(0..9).map do |d|
(10 ** (place - 2)..((10 ** (place - 1)) - 1)).sum do
Math.log10(1 + 1.fdiv(10 * _1 + d))
end
end
@havenwood
havenwood / client.rb
Created October 23, 2022 14:25
A Rack 3 example of bidirectional streaming with Async HTTP
View client.rb
# frozen_string_literal: true
require 'async/http/internet'
Async do
internet = Async::HTTP::Internet.new
response = internet.get('http://localhost:9292')
stream = response.connection.stream
stream.write('Hello World!')
@havenwood
havenwood / integer_prime.rb
Created October 13, 2022 22:49
Comparing 42.prime? to Prime.prime?(42) for Ruby IRC
View integer_prime.rb
class Integer
def prime?
return self >= 2 if self <= 3
if (bases = miller_rabin_bases)
return miller_rabin_test(bases)
end