Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
View async_fetch.rb
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 Sep 17, 2020
My `~/.pryrc` file
View .pryrc
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 / to_struct.rb
Created Sep 1, 2020
An anonymous Struct in pure Ruby (looking forward to real ones in Ruby 3)
View to_struct.rb
module ToStruct
refine Hash do
def to_struct(class_name = nil)
if class_name
Object.const_set(class_name, Struct.new(*keys))
Object.const_get(class_name).new(*values)
else
Struct.new(*keys).new(*values)
end
end
@havenwood
havenwood / same_generation_and_age.rb
Created Aug 27, 2020
A guess at what old_relik was looking for in #ruby IRC
View same_generation_and_age.rb
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 Aug 26, 2020
Clarifying a Question on #ruby IRC
View old_relik.rb
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 Jul 21, 2020
Refactoring suggestions for memcorrupt #ruby IRC
View status.rb
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 Jul 14, 2020
Array#repeated_sample implementation example for #ruby IRC
View repeated_sample.rb
# 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 Jul 7, 2020
An example HTTP gem connection pool extracted from some of my code for #ruby IRC
View connection.rb
@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 Jun 30, 2020
Tracking some packages
View tracker.rb
# frozen_string_literal: true
module Tracker
FILENAME = 'tracking.txt'
SEPARATOR = ': '
OPEN_SESAME = 'open'
OPENING = "Opening %<item>s ...\n"
module_function
@havenwood
havenwood / carc.rb
Created May 13, 2020
An example showing the carc.in API
View carc.rb
# frozen_string_literal: true
##
# curl --request POST \
# --header "Accept: application/json" \
# --header "Content-Type: application/json" \
# --data "{\"run_request\":{\"language\":\"ruby\",\"version\":\"2.7.0\",\"code\":\"p 2 + 2\"}}" \
# https://carc.in/run_requests
require 'net/http'
You can’t perform that action at this time.