Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 18:07 (UTC -07:00)
View GitHub Profile
@havenwood
havenwood / async.rb
Created November 16, 2019 15:06
Example Async equivalent to code naftilos76's code that phaul is helping with on #ruby IRC: https://gist.github.com/phaul/2b2166c913452f010b1513a8165ab287
require 'async'
condition = Async::Condition.new
Async do
task_one = Async do
loop do
puts 'task one before waiting'
condition.wait
end
@havenwood
havenwood / node.rb
Last active November 13, 2019 18:28
Playing with Enumerator.produce from Ruby 2.7
# frozen_string_literal: true
class Node
attr_reader :value
attr_reader :parent
def initialize(value:, parent: nil)
@value = value
@parent = parent
end
@havenwood
havenwood / erb.rb
Created November 12, 2019 22:51
Example YAML template for Net on #ruby channel IRC
require 'erb'
require 'psych'
topic = :users
users = %w[wombat fox poodle]
template = ERB.new <<~END, trim_mode: '-'
---
:<%= topic %>:
<%- users.each do |user| %>
@havenwood
havenwood / make_it_work.rb
Created November 7, 2019 06:27
An answer to crella133's question in #ruby IRC
class Foo
def initialize(a, b, c:)
puts 'Foo!'
end
end
class MakeItWork < Foo
def initialize(*args, **kwargs, &block)
parameters = method(__method__).super_method.parameters
class ResponseEnumerator < Enumerator
DEFAULT_PER_PAGE = 25
attr_accessor :per_page
def initialize(square, http_verb:, path:, data:,
fields:, parameters: {}, memoize: true, per_page: nil)
@per_page = per_page || DEFAULT_PER_PAGE
@memoized_responses = []
@params = parameters.fetch :params
@havenwood
havenwood / cipher.rb
Created November 5, 2019 16:12
Ruby Example of AES-256 in CBC Mode for consti on #ruby IRC
secret = 'cicada'
key = 'wombat'
cipher = OpenSSL::Cipher::AES256.new :CBC
cipher.encrypt
salt = OpenSSL::Random.random_bytes 16
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1 key, salt, 20000, 32
iv = cipher.random_iv
@havenwood
havenwood / delegate_class.rb
Last active October 20, 2019 14:39
An example using DelegateClass with core Classes. For the why, see: https://words.steveklabnik.com/beware-subclassing-ruby-core-classes
class FerretString < String
def to_s
'Successfully overrode ...'
end
end
class WombatString < DelegateClass(String)
def initialize
super('')
end
@havenwood
havenwood / sig.rb
Created October 1, 2019 13:25
An example for Nathanael7 on #ruby IRC
def sig(method_name)
meth = method(method_name)
parameters = meth.parameters.map do |type, name|
case type
when :req
name
when :rest
"*#{name}"
when :opt
# Unknown default argument.
@havenwood
havenwood / spike.rb
Last active April 11, 2024 14:02
A spike showing compiling IR binaries for all gems
# frozen_string_literal: true
require 'fileutils'
require 'find'
require 'zlib'
binaries_dir = File.join Gem.dir, 'binaries'
gems_dir = File.join Gem.dir, 'gems'
Dir['*/', base: gems_dir].each do |gem|
@havenwood
havenwood / heater.rb
Created September 26, 2019 11:54
Response to nima_m on #ruby IRC asking how to implement this state machine in Ruby: https://pasteboard.co/Izbzhuz.png
# frozen_string_literal: true
class Heater
STATE = {
s1: {heater: false, cooler: false},
s2: {heater: false, cooler: true},
s3: {heater: true, cooler: false}
}.freeze
attr_reader :state