Skip to content

Instantly share code, notes, and snippets.


Shannon Skipper havenwood

View GitHub Profile
havenwood / multiple_instances_of_state.rb
Last active Apr 26, 2021
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
def foo
havenwood / procs_blocks_and_lambdas.rb
Last active Apr 26, 2021
A few examples of Ruby's procs, blocks and lambdas (irc question)
View procs_blocks_and_lambdas.rb
def meth &block
block = meth { 42 }
prok = proc { 42 }
lamb = lambda { 42 }
#=> 42
havenwood /
Last active Apr 7, 2021
Install the latest stable Ruby as a local user (non-root).
# A non-root installation of the latest Ruby with chruby
# Install chruby (
mkdir -p $HOME/src
cd $HOME/src
wget -O chruby-0.3.9.tar.gz
tar -xzvf chruby-0.3.9.tar.gz
cd chruby-0.3.9/
PREFIX=$HOME/.chruby make install
View notes.rb
# frozen_string_literal: true
require 'sequel'
require 'stringio'
require 'zlib'
module Notes
PATH = 'Library/Group Containers/'
DB = Sequel.sqlite File.join Dir.home, PATH
TRAILING_DIVIDOR = "\u001A\u0010\n"
havenwood / range_with_indeterminate_exclusion.rb
Last active Mar 4, 2021
Does your Range exclude end? Make it a surprise!
View range_with_indeterminate_exclusion.rb
require 'fiddle'
class RangeWithIndeterminateExclusion < Range
def initialize min, max
pointer = Fiddle.dlwrap self do
loop do
sleep rand
pointer[Fiddle::SIZEOF_LONG * 4] ^= true.object_id
havenwood / hash_defaults.rb
Last active Jan 14, 2021
Ruby default values for Hashes (irc question)
View hash_defaults.rb
# A standard Hash with default proc, which in this case sets the value to the key.
default_value_to_key = { |hash, key| hash[key] = key }
#=> :nope
# This is the same as the above proc with a Hash literal.
another_value_to_key = {}
another_value_to_key.default_proc = ->(hash, key) { hash[key] = key }
havenwood / oh_dear.rb
Created Jan 13, 2021
An, ahem, answer, of sorts for how to change default IRB colors (irc question)
View oh_dear.rb
changes = {IRB::Color::BLUE => IRB::Color::YELLOW}
IRB::Color.const_get(:TOKEN_SEQ_EXPRS).tap do |token_seq_exprs|
changes.each do |old_color, new_color|
token_seq_exprs.filter_map do |type, ((color, _), _)|
color == old_color && type
end.each do |matching_type|
token_seq_exprs.dig(matching_type, 0)[0] = new_color
havenwood / fetch.rb
Last active Dec 24, 2020
My little script to download Ruby source files in parallel with Async
View fetch.rb
#!/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 / blocks.rb
Created Dec 4, 2020
Examples to illustrate what `yield` does in Ruby
View blocks.rb
# Examples without block arguments
def example_without_block(function) + 'ay'
example_without_block ->{ 'ok' }
#=> "okay"
def example_explicit_block(&block) + 'ay'
View paint
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'optionparser'
KEYWORDS = %i[color metadata glow_in_the_dark coats].freeze
DEFAULTS = {coats: 3, glow_in_the_dark: true}.freeze