Skip to content

Instantly share code, notes, and snippets.

View aks's full-sized avatar
💭
Working

Alan Stebbens aks

💭
Working
View GitHub Profile
@aks
aks / method_wrapper.rb
Created August 23, 2024 17:26
Show how class methods can be wrapped to intercept and manage their return values without code changes in their clients.
# show how class methods can be wrapped to intercept and manage their return values without code changes in their clients.
class MyClass
class << self
def method_one
"result from method one"
end
def method_two
"result from method two"
@aks
aks / specs-for.rb
Created December 5, 2023 20:20
Ruby script to produce the spec files corresponding to the ruby file arguments.
#!/usr/bin/env ruby
# specs-for file ...
#
# Don't you just hate having to find the corresponding spec file for some files
# you just updated? And, you're not using Guard (for reasons)?
#
# pass all the files you touches as arguments into this script, and it will
# issue all the corresponding spec files.
#
# If you have "ag" installed, it will be used to find the files, otherwise, the
@aks
aks / exception-class-test.rb
Created October 20, 2023 19:48
Ruby script to show sub-class exceptions rescued before parent class exceptions
#!/usr/bin/env ruby
require 'awesome_print'
class Error < StandardError; end
class SpecificError < Error ; end
class Tester
def self.run
raise SpecificError
@aks
aks / ar-mem-size.rb
Created June 27, 2023 00:07
ActiveRecord memory size usage (minimal and all)
#!/usr/bin/env ruby
require 'get_process_mem'
def mem_usage(label)
Process.fork do
m_before = mem_size
yield
m_after = mem_size
$stderr.printf "%30s: delta: %+3d MB total: %5d MB\n", label, (m_after - m_before), m_after
@aks
aks / git-aliases.sh
Created June 22, 2023 22:08
git-aliases.sh
# Git helpers
alias gadd='git add'
alias gci='git commit'
alias gco='git checkout'
alias gdiff='git diff'
alias gpull='git pull'
alias gpullhead='git pull origin $(git rev-parse --abbrev-ref HEAD)'
alias gphd='gpullhead'
alias gpush='git push'
@aks
aks / top-committers
Last active April 8, 2023 01:52
Bash script to show the top-committers in a repo, in descending frequency of commits
#!/usr/bin/env bash
# top-committers [NUMBER]
count=${1:-200}
git log --pretty=format:"%ae %at" |
awk 'BEGIN { FS=" "; OFS="," }
!seen[$1]++ { first[$1]=$2 }
{ last[$1]=$2; count[$1]++ }
END {
@aks
aks / rails-cred.sh
Last active September 4, 2024 18:01
Script to help manage, create, list, and cycle Rails environment credentials and their keys
#!/usr/bin/env bash
# rails-cred [show|edit|cycle|init|check|list] [env|all]
#
# Makes showing, editing, checking, and cycling Rails credentials
# and their secrets easier
#
# Copyright 2023 Alan K. Stebbens <aks@stebbens.org>
PROG="${0##*/}"
DIR="${0%/*}"
@aks
aks / ruby-rescue-ordering-test-run.out
Last active December 16, 2022 19:50
Show that rescue ordering determines exception handling
❯ ./ruby-rescue-ordering-test.rb
Next? [eis] e
StandardError handler # Encoding is a subclass of StandardError
this is an encoding error
Next? [eis] i
StandardError handler # IOError is also a subclass of StandardError
this is an IO error
Next? [eis] s
ScriptError handler # ScriptError is NOT a subclass of StandardError
this is a script error
require 'awesome_print'
class Foo
def test(**kwargs)
ap kwargs
end
def self.tester(label, &block)
STDERR.printf "%12s: ", label
begin
#!/bin/bash
#
# add ssh keys
PROG=${0##*/}
KEY_DIR=~/.ssh
PUB_SFX='.pub'
# on non-MacOS systems, comment this line out