Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 17:56 (UTC -07:00)
View GitHub Profile
@havenwood
havenwood / fiberator.rb
Created January 20, 2020 15:49
My WIP implementation of Enumerator backed by a Fiber (like the actually are) but in pure Ruby
# 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 January 15, 2020 22:20
A look at how to tally a collection in Ruby, from Ruby 1.9 to 2.7.
# 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 January 9, 2020 02:06
Not advocating doing this—just think it's interesting
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 January 8, 2020 03:01
My refinement suggestion for a #ruby IRC question about monkey patching
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 January 3, 2020 08:07
My solution to a #ruby IRC question
# 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 June 12, 2026 16:43
An example showing time now slipping, as long as job takes less time than interval
require 'async'
require 'async/notification'
def do_a_thing_lasting(duration)
puts 'hi'
sleep duration
puts 'bye'
end
def now_and_every(interval)
@havenwood
havenwood / download_ruby_tarballs
Created December 22, 2019 23:25
Download the latest Ruby tarballs with async-http: https://github.com/socketry/async-http
#!/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'
@havenwood
havenwood / irc_example.rb
Last active December 14, 2019 06:31
An example for IRC
chars = str
.split
.map(&:chars)
.reduce(&:zip)
.each_slice(2)
.map(&:join)
.map(&:chars)
chars.map { |char| char.map(&HASH) }
@havenwood
havenwood / object.rb
Created December 6, 2019 06:39
Examples of defining methods and getting and setting instance variables on objects
foo = Object.new
def foo.value
42
end
foo.value
#=> 42
bar = Object.new
@havenwood
havenwood / optional.rb
Created December 1, 2019 18:19
Playing with chaining #then on a Swift-like Optional
# frozen_string_literal: true
class Optional
attr_reader :value
def initialize(value)
@value = value
end
def then(&block)