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 / enumerable.rb
Last active April 24, 2022 20:26
Implementing #each with Ractor- and Async-based concurrency for fun
require 'async'
module Enumerable
module Async
[Array, Hash, Range, Struct].each do |collection|
refine collection do
def each(&block)
super do |value|
Async do
block.call(value)
@havenwood
havenwood / task.rb
Last active February 12, 2022 17:42
Just a overly simplified example of calling methods without arguments concurrently with Async and in parallel with Ractors
require 'async'
module Task
module_function
def async(*method_names)
Async do
method_names.map do |job|
Async do
job.to_proc.call(self)
@havenwood
havenwood / tic_tac_toe.rb
Created December 7, 2021 20:03
Matrix Tic Tac Toe for fun
# frozen_string_literal: true
require 'matrix'
class TicTacToe
BOARD_SIZE = 3
MARKS = {x: -1, o: 1}.freeze
private_constant :MARKS
ROW_MOVES = %i[top middle bottom].freeze
private_constant :ROW_MOVES
@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
# When Math.sqrt(2).fdiv(2) is just too zippy.
require 'bigdecimal'
require 'bigdecimal/util'
def i_ish(precision:)
sqrt_2 = 2.to_d.sqrt precision
sqrt_i = Complex sqrt_2 / 2, sqrt_2 / 2
sqrt_i * sqrt_i
end
@havenwood
havenwood / color.rb
Last active March 3, 2022 21:03
A spike implementing Crystal's enum in Ruby
require_relative 'enum'
enum :Color do
value :Red # 0
value :Green # 1
value :Blue, 5 # overwritten to 5
value :Yellow # 6 (5 + 1)
def red?
self == Color::Red
@havenwood
havenwood / notes.rb
Last active April 3, 2021 00:08
Playing with grabbing some notes from Apple Notes (See https://www.swiftforensics.com/2018/02/reading-notes-database-on-macos.html)
# frozen_string_literal: true
require 'sequel'
require 'stringio'
require 'zlib'
module Notes
PATH = 'Library/Group Containers/group.com.apple.notes/NoteStore.sqlite'
DB = Sequel.sqlite File.join Dir.home, PATH
TRAILING_DIVIDOR = "\u001A\u0010\n"
@havenwood
havenwood / hash_defaults.rb
Last active January 14, 2021 01:34
Ruby default values for Hashes (irc question)
##
# A standard Hash with default proc, which in this case sets the value to the key.
default_value_to_key = Hash.new { |hash, key| hash[key] = key }
default_value_to_key[:nope]
#=> :nope
##
# This is the same as the above proc with a Hash literal.
another_value_to_key = {}
another_value_to_key.default_proc = ->(hash, key) { hash[key] = key }
@havenwood
havenwood / oh_dear.rb
Created January 13, 2021 07:22
An, ahem, answer, of sorts for how to change default IRB colors (irc question)
changes = {IRB::Color::BLUE => IRB::Color::YELLOW}
IRB::Color.const_get(:TOKEN_SEQ_EXPRS).tap do |token_seq_exprs|
changes.each do |old_color, new_color|
token_seq_exprs.filter_map do |type, ((color, _), _)|
color == old_color && type
end.each do |matching_type|
token_seq_exprs.dig(matching_type, 0)[0] = new_color
end
end
@havenwood
havenwood / fetch.rb
Last active December 24, 2020 19:46
My little script to download Ruby source files in parallel with Async
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'async'
require 'async/barrier'
require 'async/http/client'
require 'async/http/endpoint'
require 'async/logger'
require 'optionparser'