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 / 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.
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)
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 / paint
Last active January 6, 2023 01:10
A Ruby stdlib optparse example with paint
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'optionparser'
KEYWORDS = %i[color metadata glow_in_the_dark coats].freeze
DEFAULTS = {coats: 3, glow_in_the_dark: true}.freeze
@havenwood
havenwood / config.ru
Last active January 3, 2023 02:59
An example Rack app
# frozen_string_literal: true
##
# Example ERB template
require 'erb'
TEMPLATE = <<~HTML
<!DOCTYPE html>
<html lang="en">
<head>
@havenwood
havenwood / undigits.rb
Last active November 19, 2022 03:34
Array#undigits
module Undigits
refine Array do
def undigits(base = 10)
reverse.reduce(0) do |acc, digit|
acc * base + digit
end
end
end
end
@havenwood
havenwood / puts_to_null.rb
Last active November 15, 2022 18:37
Send puts output to dev/null inside a block
def dev_null
File.open(File::NULL, 'w') do |null|
$stdout = null
yield
ensure
$stdout = STDOUT
end
end
dev_null do
@havenwood
havenwood / cache.rb
Created March 20, 2020 20:13
Another example for xco on #ruby IRC
require_relative 'tuple_space'
class Cache
def initialize
@memory = TupleSpace.new(reaper_period_in_secs: 10, expires_in_secs: 60)
end
def get(request)
@memory[request]
end
@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!')