Skip to content

Instantly share code, notes, and snippets.

@floehopper
floehopper / validate-parameters-against-method-signature.rb
Created August 14, 2022 11:22
Validate parameters against method signature
def valid?(method, ...)
signature = method.inspect
pattern = %r{(?:\.|#)#{method.name}\(([^\)]*)\)}
params = signature.match(pattern)[1]
params_with_nil_default_values = params.gsub('...', 'nil')
klass = Class.new
klass.class_eval "def self.#{method.name}(#{params_with_nil_default_values})\nend\n"
klass.public_send(method.name, ...)
true
rescue ArgumentError => e
@floehopper
floehopper / display_changed_row_counts.rb
Created February 1, 2022 14:52
Display changed row counts using ActiveRecord
def row_counts
Hash[*ApplicationRecord.connection.execute(%{
ANALYZE;
SELECT
pgClass.relname AS tableName,
pgClass.reltuples AS rowCount
FROM
pg_class pgClass
INNER JOIN
pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
@floehopper
floehopper / flatten
Created December 27, 2021 11:26
Flatten files in sub-directories into single directory
#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
pwd = Pathname.pwd
begin
source = Pathname.new(ARGV[0].nil? ? pwd : ARGV[0]).realpath
destination = Pathname.new(ARGV[1].nil? ? pwd: ARGV[1]).realpath
@floehopper
floehopper / zx81-print-hi.bas
Created November 22, 2020 11:39
ZX81 machine code program to print "HI"
10 REM 000000000000
20 POKE 16514, 1
30 POKE 16515, 141
40 POKE 16516, 64
50 POKE 16517, 17
60 POKE 16518, 2
70 POKE 16519, 0
80 POKE 16520, 205
90 POKE 16521, 107
100 POKE 16522, 11
@floehopper
floehopper / ec2-iam-security-credentials.md
Last active November 7, 2020 15:09
Obtaining security credentials derived from IAM role on EC2 machine
@floehopper
floehopper / install.md
Last active August 31, 2020 10:05
Installing OSX Catalina from scratch

List & install software updates

softwareupdate --list
sudo softwareupdate --verbose --install --all

Set computer name

COMPUTER_NAME="<computer-name>"
sudo scutil --set ComputerName $COMPUTER_NAME
@floehopper
floehopper / Vagrantfile
Last active October 22, 2021 20:00
Ubuntu Xenial with Ruby installed via rbenv
PROVISION_SYSTEM = <<~EOS
set -e
apt-get update --yes
apt-get install --yes gcc
apt-get install --yes make
EOS
PROVISION_USER = <<~EOS
set -e
mkdir -p ~/.rbenv
@floehopper
floehopper / testing_before_filter_in_controller_spec.rb
Created July 4, 2019 15:27
Testing before_filter in controller spec
require 'rails_helper'
RSpec.describe MyController do
describe 'GET #any_action' do
controller do
def any_action
head :ok
end
end
@floehopper
floehopper / update-git-author-date-and-commiter-date-to-current-time.sh
Last active February 20, 2025 08:33
Update git author-date and committer-date to current time with 1 second delay between commits
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --env-filter 'export GIT_COMMITTER_DATE="$(date)"; export GIT_AUTHOR_DATE="$(date)"; sleep 1' main..HEAD