Skip to content

Instantly share code, notes, and snippets.

View kjlape's full-sized avatar
🤝
nice to meet you

Kaleb Lape kjlape

🤝
nice to meet you
View GitHub Profile
@kjlape
kjlape / keybase.md
Created August 1, 2019 19:20
Proof of GitHub account ownership for https://keybase.io

Keybase proof

I hereby claim:

  • I am pingortle on github.
  • I am pingortle (https://keybase.io/pingortle) on keybase.
  • I have a public key ASDdzMBjB_KEboIXEmrJvAuon9g6lIZR3Z0KREwT5svJUAo

To claim this, I am signing this object:

@kjlape
kjlape / sorting_item_list_by_id_list.rb
Last active June 14, 2019 17:26
Sorting lists with lists in ruby
# Given #1: a list of ids in a specific order you want to preserve
ordered_ids = (1..3).to_a.reverse
#=> [3, 2, 1]
indexed_ids = ordered_ids.each_with_index.to_h
#=> {3=>0, 2=>1, 1=>2}
# Given #2: a collection of items with the same ids as in the list above, but in no particular order
# Example: `Model.where(…)` in rails
Model = Struct.new(:id, :name)
items = (1..3).map { |id| Model.new(id, "Item #{id}") }
@kjlape
kjlape / active_job_listening_to_errors.rb
Created June 11, 2019 18:04
[WIP][POC] Passively listen to active job errors. This shouldn't disrupt the normal retry_on/discard_on flow. It allows a hook for you to observe errors.
module ActiveJobListeningToErrors
extend ActiveSupport::Concern
module ClassMethods
def on_error(error_class)
around_perform do |job, perform_block|
perform_block.call
rescue error_class => error
yield job, error
raise
@kjlape
kjlape / activejob-instrumentation-changes-5_2-to-6_0.md
Last active May 20, 2019 20:32
Surprising ActiveJob instrumentation changes from 5.2 to 6.0

The behavior of enqueue.active_job changed slightly between Rails 5.2 and 6.0. In 5.2 the event doesn't fire if there's an error queuing the job. In 6.0 it always fires regardless of error.

Relevant rails code is…

One more interesting difference between 5.2 and 6.0 here is that 5.2 did not include timing information since it fires in an after_enqueue. In 6.0 it has moved to around_enqueue and passes the block to be instrumented.

if Gem::Version.new("2.6") <= Gem::Version.new(RUBY_VERSION)
def compose(*funcs)
funcs.map(&:to_proc).reduce(:<<)
end
else
def compose(*funcs)
funcs.map(&:to_proc).reduce { |f, g| ->(*x) { f.call(g.call(*x)) } }
end
end
@kjlape
kjlape / quick-bench.rb
Created December 19, 2018 18:46
Quick n' dirty benchmarking + asserting in ruby scripts
require "benchmark"
require "test/unit/assertions"
extend Test::Unit::Assertions
text = <<END
here's some
text for
you
@kjlape
kjlape / gloan.sh
Last active August 20, 2018 15:23 — forked from searls/gloan.sh
Clone into a new repo and quickly switch into it. Helps avoid a haphazard collection of unorganized github repos all over my home directory. Once it's cloned (or even if it errors), just hit Paste & it'll change into the directory of the repo.
#!/bin/sh -e
# A simple script to keep a tidy ~/code directory organized by owner & then repo
# When the script is done, just hit command-v to switch into the directory
# (Github and Mac only. Sorry, openness!)
#
# Usage:
# gloan <org>/<repo>
# Or:
# gloan <org> <repo>
@kjlape
kjlape / setup.sh
Last active August 20, 2018 17:00
#!/bin/bash
set -e
# Install brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew install rbenv
# navigate to the boot partition
# set WPA_SSID and WPA_PSK
touch ssh
cat > wpa_supplicant.conf <<EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
@kjlape
kjlape / ll.js
Created March 5, 2018 12:46
Linked List in JS
function Node(data, { left, right } = {}) {
this.data = data
this._left = left || this,
this._right = right || this
}
Node.prototype = {
left,
right,
insertLeft,