Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Leafly
  • Ashland, Oregon
  • 17:29 (UTC -07:00)
View GitHub Profile
@havenwood
havenwood / country_emoji.rb
Last active March 24, 2020 22:31
Country Emoji
COUNTRY_LETTERS = 'A'.upto('Z').with_index(127462).to_h.freeze
def country_emoji(iso)
COUNTRY_LETTERS.values_at(*iso.chars).pack('U*')
end
country_emoji('GB')
#=> "🇬🇧"
string = 'numbers: one two three # en fin'
caption = string[/\A\w*/]
content = string[/(?<=:).*(?=#)/].split
caption
#=> "numbers"
content
#=> ["one", "two", "three"]
@havenwood
havenwood / cache.rb
Created March 20, 2020 20:13
Another example for xco on #ruby IRC
require_relative 'tuple_space'
class Cache
def initialize
@memory = TupleSpace.new(reaper_period_in_secs: 10, expires_in_secs: 60)
end
def get(request)
@memory[request]
end
@havenwood
havenwood / xco.rb
Created March 20, 2020 19:57
Example for xco on #ruby IRC
class Xco
attr_accessor :reaper
def initialize
@reaper = Automaton.new self
@reaper.start
end
class Automaton
def initialize(source, function: :reap, interval: 1)
@havenwood
havenwood / iterate_string.rb
Created March 6, 2020 16:48
My solution to phaul's kata mentioned in #ruby IRC: https://www.codewars.com/kata/59b7b7f0623654c2ce000021/ruby
module IterateString
refine String do
def iterate(count, &block)
Enumerator.produce(self, &block).first(count)
end
end
end
using IterateString
# frozen_string_literal: true
require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1_000_000
IRB.conf[:PROMPT_MODE] = :SIMPLE
@havenwood
havenwood / multiple_instances_of_state.rb
Last active March 11, 2022 17:51
Examples of a module, class and singleton class in Ruby
class MultipleInstancesOfState
attr_accessor :state
def initialize(state:)
@state = state
end
def foo
@state.reverse!
end
@havenwood
havenwood / isolates.sh
Created February 18, 2020 17:04
Isolates in Ruby (an early look at ko1's work)
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 January 25, 2020 00:36
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
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 January 23, 2020 05:01
Benchmark of various solutions to a question on #ruby IRC
# 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) }