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 / multiple_instances_of_state.rb
Last active Feb 21, 2020
Examples of a module, class and singleton class in Ruby
View multiple_instances_of_state.rb
class MultipleInstancesOfState
attr_accessor :state
def initialize(state:)
@state = state
end
def foo
@state.reverse!
end
@havenwood
havenwood / isolates.sh
Created Feb 18, 2020
Isolates in Ruby (an early look at ko1's work)
View isolates.sh
git clone --depth 1 --single-branch --branch guild https://github.com/ko1/ruby.git ruby-isolates-src
cd ruby-isolates-src
autoconf
./configure --prefix=$HOME/.rubies/ruby-guilds # chruby
# ./configure --prefix=$HOME/.rvm/rubies/ruby-guilds # RVM
# ./configure --prefix=$HOME/.rbenv/versions/3.0.0/ruby-guilds # rbenv
make -j2
make install
@havenwood
havenwood / fix_strptime.rb
Last active Jan 25, 2020
A refinement to fix the Time::strptime lack of %U/%u/%W/%w/%V/%g/%G support pre-2.7 — see https://github.com/ruby/ruby/commit/a9d4f2d03c847ec1c89dc03a5076a9fa29ffa61f#diff-0ead6351265632d68f106a2b1a98f5f9
View fix_strptime.rb
require 'time'
module FixStrptime
refine Time.singleton_class do
def strptime(date, format, now=self.now)
d = Date._strptime(date, format)
raise ArgumentError, "invalid date or strptime format - `#{date}' `#{format}'" unless d
if seconds = d[:seconds]
if sec_fraction = d[:sec_fraction]
usec = sec_fraction * 1000000
@havenwood
havenwood / benchmark.rb
Created Jan 23, 2020
Benchmark of various solutions to a question on #ruby IRC
View benchmark.rb
# frozen_string_literal: true
require 'benchmark/ips'
Benchmark.ips do |ips|
words = [*'a'..'all'].freeze
separator = '-'
ips.report('map & join') { words.map { |word| separator + word }.join }
ips.report('join & prepend') { words.join(separator).prepend(separator) }
@havenwood
havenwood / fiberator.rb
Created Jan 20, 2020
My WIP implementation of Enumerator backed by a Fiber (like the actually are) but in pure Ruby
View fiberator.rb
# frozen_string_literal: true
require 'delegate'
class Fiberator < DelegateClass Fiber
class Rewind < StandardError
end
NOTHING = Object.new
private_constant :NOTHING
@havenwood
havenwood / tally.rb
Created Jan 15, 2020
A look at how to tally a collection in Ruby, from Ruby 1.9 to 2.7.
View tally.rb
# Ruby 1.9
Hash[collection.group_by { |n| n }.map { |k, v| [k, v.size] }]
# Ruby 2.0 Array#to_h
collection.group_by { |n| n }.map { |k, v| [k, v.size] }.to_h
# Ruby 2.2 Object#itself
collection.group_by(&:itself).map { |k, v| [k, v.size] }.to_h
# Ruby 2.4 Hash#transform_values
@havenwood
havenwood / red_inspect.rb
Created Jan 9, 2020
Not advocating doing this—just think it's interesting
View red_inspect.rb
module RedInspect
REFINED = [NilClass, Array, Array.singleton_class].freeze
def inspect
"\e[91m#{super}\e[0m"
end
self.then do |oneself|
REFINED.each { refine(_1) { include oneself } }
end
@havenwood
havenwood / size_squared.rb
Last active Jan 8, 2020
My refinement suggestion for a #ruby IRC question about monkey patching
View size_squared.rb
module SizeSquared
[String, Enumerable].each do |this_class|
refine this_class do
def size_squared
size.abs2
end
alias length_squared size_squared
end
end
end
@havenwood
havenwood / jekyll_plugins.rb
Last active Jan 3, 2020
My solution to a #ruby IRC question
View jekyll_plugins.rb
# frozen_string_literal: true
fetcher = Gem::SpecFetcher.fetcher
matches = fetcher.detect do |spec|
spec.name.start_with? 'jekyll-'
end
plugins = matches.map(&:first).map(&:name).uniq
@havenwood
havenwood / now_and_every.rb
Last active Dec 24, 2019
An example showing time now slipping, as long as job takes less time than interval
View now_and_every.rb
require 'async'
require 'async/notification'
def do_a_thing_lasting(duration)
puts 'hi'
sleep duration
puts 'bye'
end
def now_and_every(interval)
You can’t perform that action at this time.