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 / example.rb
Created August 31, 2022 00:10
Rust-inspired `struct` and `impl` in Ruby for fun
require_relative 'kernel'
struct :Point do
{
x: 0,
y: 0,
z: 0
}
end
@havenwood
havenwood / arithmetic_sequence.rb
Last active March 29, 2024 00:22
An example of implementing a JSON addition to support Enumerator::ArithmeticSequence
# frozen_string_literal: true
require 'json' unless defined?(JSON::JSON_LOADED) && JSON::JSON_LOADED
class Enumerator
class ArithmeticSequence
# See #as_json.
def self.json_create(object)
Range.new(*object.values_at('b', 'e', 'x')) % object['s']
end
@havenwood
havenwood / atomic_move.rb
Created August 12, 2022 17:56
Example of atomic move for #ruby IRC
require 'fileutils'
require 'tempfile'
def write_atomically(content, temp_name: 'temp.txt', atomic_path: 'atomic.txt')
temp = Tempfile.new temp_name
temp.write content
temp.close
FileUtils.mv temp.path, atomic_path
ensure
@havenwood
havenwood / each.rb
Created July 5, 2022 18:44
An example of why we use `each` rather than `for` and what semicolon block args do for #ruby IRC
foo = :untouched
bar = :untouched
[:touched].each do |foo|
bar = :touched
end
foo #=> :untouched
bar #=> :touched
@havenwood
havenwood / class_mixin.rb
Created July 3, 2022 00:49
My example for Ruby #irc showing how you can contort and mixin class methods.
require 'digest/md5'
class_name = Digest::MD5
module class_name::Mixin
end
class_name::Mixin.class_eval do
class_name.public_methods(false).each do |method_name|
define_method method_name, class_name.public_method(method_name).to_proc
end
@havenwood
havenwood / skynet.rb
Created May 3, 2022 00:14
A port of the Crystal "skynet" code to Ruby, see https://bugs.ruby-lang.org/issues/18760
require 'async'
require 'async/queue'
def skynet(c, num, size, div)
if size == 1
c << num
else
rc = Async::LimitedQueue.new(div)
sum = 0
div.times do |i|
@havenwood
havenwood / example.rb
Last active April 29, 2022 14:03
Kaprekar's Constant in Ruby
require_relative 'kaprekar'
include Kaprekar
p kaprekar(four_digit_number: 9000).first(10)
#>> [9000, 8991, 8082, 8532, 6174, 6174, 6174, 6174, 6174, 6174]
p kaprekar(four_digit_number: 9000).take_while { _1 != Kaprekar::CONSTANT }
#>> [9000, 8991, 8082, 8532]
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'rack', github: 'rack/rack'
gem 'puma', github: 'puma/puma'
@havenwood
havenwood / store.rb
Created February 8, 2022 05:32
Handling nested PStore transactions (IRC example)
require 'pstore'
class Store
def initialize(relative_path: '.data.pstore')
@store = PStore.new(relative_path, true)
@store.ultra_safe = true
@store.transaction(true) { }
@in_transaction = false
@store
end
@havenwood
havenwood / async.rb
Last active February 12, 2022 17:43
require 'async'
class Enumerator
class Async
module Refinement
refine Enumerable do
def async
Enumerator::Async.new(self)
end
end