Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / carc.rb
Created May 13, 2020
An example showing the carc.in API
View carc.rb
# frozen_string_literal: true
##
# curl --request POST \
# --header "Accept: application/json" \
# --header "Content-Type: application/json" \
# --data "{\"run_request\":{\"language\":\"ruby\",\"version\":\"2.7.0\",\"code\":\"p 2 + 2\"}}" \
# https://carc.in/run_requests
require 'net/http'
@havenwood
havenwood / .pryrc
Last active May 12, 2020
A snippet to add to your .pryrc so refinements work at top level
View .pryrc
Tempfile.new('pry-binding').tap do |binding_chord|
binding_chord << 'Pry.toplevel_binding = binding'
binding_chord.close
load binding_chord.path
ensure
binding_chord.unlink
end
@havenwood
havenwood / memoized.rb
Last active May 9, 2020
Enumerable#memoized so Enumerable#lazy doesn't get lonely
View memoized.rb
# frozen_string_literal: true
class Enumerator
class Memoized < Enumerator
INTERRUPT = defined?(IRB::Abort) ? IRB::Abort : Interrupt
private_constant :INTERRUPT
def initialize(enum)
@original_inspect = enum.inspect
@enum = cloned(enum)
View enumerator-memoized.rb
# frozen_string_literal: true
class Enumerator
class Memoized < Enumerator
INTERRUPT = defined?(IRB::Abort) ? IRB::Abort : Interrupt
private_constant :INTERRUPT
module Refinement
refine Enumerable do
def memoized
@havenwood
havenwood / .zshrc
Created May 7, 2020
My ~/.zshrc file
View .zshrc
##
# zsh
autoload colors && colors
autoload -Uz compinit && compinit -i
autoload -Uz add-zsh-hook
setopt auto_cd cdablevars beep nomatch notify
setopt completeinword extendedglob globdots prompt_subst interactivecomments
setopt appendhistory sharehistory incappendhistory hist_ignore_space hist_ignore_all_dups
@havenwood
havenwood / .irbrc
Created May 7, 2020
My ~/.irbrc irb settings file
View .irbrc
# frozen_string_literal: true
require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100_000_000
IRB.conf[:EVAL_HISTORY] = 100_000_000
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:PROMPT][:SIMPLE][:PROMPT_C] = '| '
# Use a proc rather than function for context to get refinements working.
@havenwood
havenwood / dry_product.rb
Last active Apr 21, 2020
Example of implementing a simple #dig for #ruby IRC
View dry_product.rb
# For "fun," a DRY metaprogrammed version
class Product
ATTRIBUTES = %i[name price]
ATTRIBUTES.each { |attribute| attr_reader attribute }
def initialize(**keywords)
missing = ATTRIBUTES - keywords.keys
if missing.any?
message = "missing keywords: #{missing}.map(&:inspect).join(', ')"
@havenwood
havenwood / before_all.rb
Created Apr 5, 2020
Before all methods hook example for stapler on #ruby IRC
View before_all.rb
class BeforeAll < Module
def initialize(before_method)
@before_method = before_method
end
def prepended(base)
base.instance_methods(false).each do |method_name|
this_method = base.instance_method(method_name)
before_method = @before_method
define_method(method_name) do |*args, **kwargs, &block|
@havenwood
havenwood / samples.rb
Last active Apr 3, 2020
Example refactor for akem on #ruby IRC
View samples.rb
#!/usr/bin/env ruby
File.open IO::NULL, 'w' do |dev_null|
File.readlines('samples.txt', chomp: true).each do |sample|
sample_speed = rand(0.1..1.0)
cmd = "play -v 1 \"#{sample}\""
puts cmd
if rand(2).zero?
Process.detach spawn(cmd, err: :out, out: dev_null)
View tilde_count.rb
module TildeCount
refine Enumerable do
alias ~@ count
end
end
using TildeCount
~[]
#=> 0
You can’t perform that action at this time.