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 / delete_archdir_prefix.patch
Last active April 24, 2024 22:20
Minimalist patch for Ruby 3.3.1 [Bug #20450] https://github.com/ruby/ruby/pull/10619
diff --git a/lib/bundled_gems.rb b/lib/bundled_gems.rb
index e756af61ea..555d1d4cd1 100644
--- a/lib/bundled_gems.rb
+++ b/lib/bundled_gems.rb
@@ -98,7 +98,7 @@ def self.warning?(name, specs: nil)
# name can be a feature name or a file path with String or Pathname
feature = File.path(name)
# bootsnap expand `require "csv"` to `require "#{LIBDIR}/csv.rb"`
- name = feature.delete_prefix(LIBDIR).chomp(".rb").tr("/", "-")
+ name = feature.delete_prefix(ARCHDIR).delete_prefix(LIBDIR).tr("/", "-")
@havenwood
havenwood / export.rb
Last active April 17, 2024 22:24
Zip example for #ruby IRC
##
# This is similar to your code to reproduce what it's doing
def export_similar_to_your_code
io = Zip::OutputStream.write_buffer do |stream|
'a'.upto 'z' do |char|
stream.put_next_entry "#{char}.txt"
stream << char * 42
end
ensure
stream.close_buffer
@havenwood
havenwood / attributes.rb
Last active April 8, 2024 00:11
My example showing dynamic top level instance variables with attrs for #ruby IRC
class Attributes < Module
def initialize(**attributes)
self.class.class_eval do
define_method :included do |klass|
klass.class_eval do
attributes.each do |key, value|
instance_variable_set "@#{key}", value
singleton_class.class_eval { attr_accessor key }
end
end
@havenwood
havenwood / array_indices.rb
Created March 27, 2024 10:54
Example Array#indices implementation in Ruby
module ArrayIndices
refine Array do
def indices(...)
positions = []
offset = 0
while (position = self[offset..].index(...)&.+(offset))
positions << position
offset = position.succ
end
@havenwood
havenwood / csv_file_model.rb
Created March 26, 2024 03:42
Example read and append CSV ActiveModel implementation for Rails
# frozen_string_literal: true
require 'active_model'
require 'active_support/inflector'
require 'csv'
class CSVFileModel
class RecordNotFound < StandardError
def message = 'Record not found.'
end
@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")