Skip to content

Instantly share code, notes, and snippets.

:octocat:

Shannon Skipper havenwood

:octocat:
Block or report user

Report or block havenwood

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@havenwood
havenwood / sig.rb
Created Oct 1, 2019
An example for Nathanael7 on #ruby IRC
View sig.rb
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
Created Sep 29, 2019
A spike showing compiling IR binaries for all gems
View spike.rb
# 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 Sep 26, 2019
Response to nima_m on #ruby IRC asking how to implement this state machine in Ruby: https://pasteboard.co/Izbzhuz.png
View heater.rb
# 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
@havenwood
havenwood / user_agents.rb
Created Sep 20, 2019
Sample a popular User-Agent based on its likelihood according to Sept 20, 2019 data from https://techblog.willshouse.com/2012/01/03/most-common-user-agents/
View user_agents.rb
require 'psych'
module UserAgent
USER_AGENTS = Psych.load_file 'user_agents.yml'
module_function
def sample
target = rand
@havenwood
havenwood / autovivicious.rb
Created Sep 10, 2019
An example of an autovivifying Hash refinement in Ruby.
View autovivicious.rb
module Vivification
refine Hash.singleton_class do
def new(*args, autovivify: false, &block)
return Hash.new(&VIVIFIED_PROC) if autovivify
super(*args, &block)
end
end
end
@havenwood
havenwood / missing_extensions.rb
Created Aug 22, 2019
Response to IRC question
View missing_extensions.rb
require 'bundler'
def lockfile
Bundler.read_file Bundler.default_lockfile
end
def lockfile_gems
Bundler::LockfileParser.new(lockfile).specs.map do |spec|
[spec.name, spec.version ]
end
@havenwood
havenwood / keyword_curry.rb
Created Aug 20, 2019
A little spike on implementing currying for keyword arguments after reading: https://bugs.ruby-lang.org/issues/16113
View keyword_curry.rb
# frozen_string_literal: true
module KeywordCurry
refine Method do
def curry(curry_arity = nil)
to_proc.curry(curry_arity)
end
end
refine Proc do
@havenwood
havenwood / enumerable.rb
Created Jul 31, 2019
Enumerable and Enumerator examples for Technodrome on IRC
View enumerable.rb
class Technodrome
include Enumerable
def each
yield :fee
yield :fi
yield :fo
yield :fum
end
end
@havenwood
havenwood / ascii_art_png.rb
Created Jul 4, 2019
A quick spike to turn ascii art into a png
View ascii_art_png.rb
# frozen_string_literal: true
require 'chunky_png'
ascii = <<~ASCII
@@@@@@ @@@@@@
@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@ @@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@havenwood
havenwood / base58.rb
Created Jun 28, 2019
Example Base58 Implementation (showing haxx0r> on #ruby irc)
View base58.rb
module Base58
CHARACTERS = [*?1..?9, *?a..?k, *?m..?z, *?A..?H, *?J..?N, *?P..?Z].freeze
module_function
def encode(n)
chars = []
while n >= 58
chars.unshift BASE58_CHARACTERS[n % 58]
You can’t perform that action at this time.