Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 19:19 (UTC -07:00)
View GitHub Profile
@havenwood
havenwood / pragma.rb
Created March 2, 2024 04:09
Fun with Ruby pragma
#/|\ ^._.^ /|\-*-shaREABle-cONStaNt_vaLUe:LITEral;🦇;fRozEN_StRING-Literal:tRUE-*-/|\ ^._.^ /|\#
FROZEN = %w[❄️]
p FROZEN.frozen?
#=> true
p FROZEN.all?(&:frozen?)
#=> true
@havenwood
havenwood / Gemfile
Last active February 28, 2024 23:59
An example of streaming a Rack response with Trenni streaming templates https://github.com/ioquatix/trenni#readme
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'trenni'
@havenwood
havenwood / config.ru
Created February 28, 2024 22:57
An example of a streaming Rack app with NoHeaders and NoBody middleware
# frozen_string_literal: true
##
# A Rack app that streams a simple HTML page.
module App
PRETEND_TO_WAIT_FOR_IO = proc do
sleep 2
'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'
end
HEADERS = {'content-type' => 'text/html; charset=utf-8'}.freeze
@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
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
# 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.
# 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
# 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.
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>