Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
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.
# 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.
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
# 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
class Integer
def prime?
return self >= 2 if self <= 3
if (bases = miller_rabin_bases)
return miller_rabin_test(bases)
end
@havenwood
havenwood / option.rb
Last active October 7, 2022 15:39
An example of a Rust-like `Option` enum in Ruby, see https://doc.rust-lang.org/rust-by-example/std/option.html
# frozen_string_literal: true
# An example of a Rust-like `Option` enum in Ruby
module Option
class NoneError < StandardError
def message = "called #unwrap on a `None' value"
end
Some = Data.define(:unwrap) do
def initialize(value) = super
@havenwood
havenwood / inline_runner.rb
Created September 28, 2022 20:32
A little script to run RuboCop on a Ruby String from within Ruby
require 'rubocop'
require 'tempfile'
class InlineRunner
DEFAULT = RuboCop::Runner.new({}, RuboCop::ConfigStore.new)
def initialize(runner: DEFAULT, filename: 'runner.rb')
@runner = runner
@file = Tempfile.new(filename)
end
@havenwood
havenwood / example.rb
Created August 31, 2022 00:10
Rust-inspired `struct` and `impl` in Ruby for fun
require_relative 'kernel'
struct :Point do
{
x: 0,
y: 0,
z: 0
}
end
@havenwood
havenwood / arithmetic_sequence.rb
Last active December 10, 2023 15:56
An example of implementing a JSON addition to support Enumerator::ArithmeticSequence
# frozen_string_literal: true
require 'json' unless defined?(JSON::JSON_LOADED) && JSON::JSON_LOADED
class Enumerator
class ArithmeticSequence
# See #as_json.
def self.json_create(object)
Range.new(*object.values_at('b', 'e', 'x')) % object['s']
end
@havenwood
havenwood / atomic_move.rb
Created August 12, 2022 17:56
Example of atomic move for #ruby IRC
require 'fileutils'
require 'tempfile'
def write_atomically(content, temp_name: 'temp.txt', atomic_path: 'atomic.txt')
temp = Tempfile.new temp_name
temp.write content
temp.close
FileUtils.mv temp.path, atomic_path
ensure