Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 01:11 (UTC -07:00)
View GitHub Profile
class GrossWeight
include Comparable
attr_reader :value
def initialize(value)
@value = value
end
def <=>(other)
require 'async'
require 'async/barrier'
require 'open-uri'
def async_fetch(*urls)
barrier = Async::Barrier.new
Async do
urls.map do |url|
barrier.async { URI.open url, &:read }
@havenwood
havenwood / .pryrc
Created September 17, 2020 18:03
My `~/.pryrc` file
Pry.config.prompt = Pry::Prompt[:simple]
Pry.config.pager = false
##
# Support refinements at top level in Pry.
Pry.hooks.add_hook :when_started, :context_switch do |_output, _binding, pry|
pry.binding_stack = [TOPLEVEL_BINDING]
end
##
@havenwood
havenwood / struct_literal.rb
Last active January 9, 2025 16:30
An anonymous Struct in pure Ruby (hoping Ruby adds a real one)
module StructLiteral
refine Hash do
def to_struct(class_name = nil)
if class_name
unless Object.const_defined?(class_name)
Object.const_set(class_name, Struct.new(*keys))
end
Object.const_get(class_name).new(*values)
else
@havenwood
havenwood / same_generation_and_age.rb
Created August 27, 2020 05:36
A guess at what old_relik was looking for in #ruby IRC
def recurse_ages(hash, depth: 0, history: [])
hash.flat_map do |key, value|
if value.instance_of?(Hash)
history[depth] = key
method(__method__).call value, depth: depth.succ, history: history
else
[history[0..depth.pred], value]
end
end
end
@havenwood
havenwood / old_relik.rb
Created August 26, 2020 22:02
Clarifying a Question on #ruby IRC
h1 = { a: true, b: { c: [1, 2, 3], d: [42] } }
h2 = { a: false, b: { x: [3, 4, 5], d: [42] } }
h1.deep_intersection(h2)
#=> ?
@havenwood
havenwood / status.rb
Created July 21, 2020 04:12
Refactoring suggestions for memcorrupt #ruby IRC
class Status
include Comparable
CODES = [['-', :todo], ['x', :doing], ['+', :done]].freeze
SYM = CODES.map(&:first).each_with_index.to_h.freeze
STR = CODES.map(&:last).each_with_index.to_h.freeze
INT = CODES.each_with_index.reduce({}) do |acc, (keys, i)|
acc.merge({i => i}, keys.to_h { |key| [key, i] })
end.freeze
@havenwood
havenwood / repeated_sample.rb
Created July 14, 2020 18:34
Array#repeated_sample implementation example for #ruby IRC
# frozen_string_literal: true
require 'securerandom'
module RepeatedSample
refine Array do
def repeated_sample(sample_size = 1)
m = 1
limit = size
while limit * size <= 0x100000000
@havenwood
havenwood / connection.rb
Created July 7, 2020 01:17
An example HTTP gem connection pool extracted from some of my code for #ruby IRC
@executor = Concurrent::ThreadPoolExecutor.new min_threads: min_threads,
max_threads: max_threads
def connection_pool
@connection_pool ||= ConnectionPool.new size: @pool_size, timeout: @pool_timeout do
HTTP.auth("Bearer #{access_token}")
.persistent(HOST)
.timeout(@http_timeout)
.headers(accept_encoding: ACCEPT_ENCODING)
.use(:auto_inflate)
@havenwood
havenwood / tracker.rb
Created June 30, 2020 23:53
Tracking some packages
# frozen_string_literal: true
module Tracker
FILENAME = 'tracking.txt'
SEPARATOR = ': '
OPEN_SESAME = 'open'
OPENING = "Opening %<item>s ...\n"
module_function