Skip to content

Instantly share code, notes, and snippets.

View JonathanWThom's full-sized avatar
🐶

Jonathan Thom JonathanWThom

🐶
View GitHub Profile
@JonathanWThom
JonathanWThom / update-everything.sh
Last active May 6, 2023 05:09
Script I use to update my Mac
#!/bin/sh
# Add or `ln -s` this into `/usr/local/bin/`
echo "Updating everything..."
echo "Updating Homebrew and all Homebrew packages"
brew update && brew upgrade && brew cleanup && brew doctor
echo "Updating all programming languages"
@JonathanWThom
JonathanWThom / shadow.rb
Last active June 12, 2021 07:15
Ruby shadowing method example
# https://thomasleecopeland.com/2017/04/20/shadowing-bug-in-the-wild.html
# https://docs.ruby-lang.org/en/2.4.0/syntax/assignment_rdoc.html#label-Local+Variables+and+Methods
# https://www.codinginthecrease.com/news_article/show/307337
class Okay
def yep
"yep"
end
def nope
@JonathanWThom
JonathanWThom / dockernuke
Created May 5, 2021 05:29
Remove all the Docker things
docker stop $(docker ps -qa); docker rm $(docker ps -qa); docker rmi -f $(docker images -qa); docker volume rm $(docker volume ls -q); docker network rm $(docker network ls -q)
@JonathanWThom
JonathanWThom / backup
Last active September 19, 2021 07:06
Sample backup script using Borg (with optional Rclone sync)
#!/bin/zsh
# Backup a folder to a remote address using borg.
# To restore: borg extract $BORG_REPO::computer-and-date
set -eu
# Will need a remote server setup with Borg, and a Borg repo first.
export BORG_REPO="<user>@<host>:<borg-repo>"
# Assuming you've installed Borg with Homebrew
@JonathanWThom
JonathanWThom / diffie-hellman.rb
Last active December 9, 2020 05:55
Naive Diffie-Hellman implementation
# Based on https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
require "prime"
require "openssl"
class Client
def initialize
@private_key = rand(1..100000)
@clients = {}
end
@JonathanWThom
JonathanWThom / fizzbuzz.rb
Created August 30, 2019 00:46
Prompt: What is the most complex and unreadable way you can write FizzBuzz?
(1..100).each do |i|
[3, 5].each do |num|
define_method("klass_#{num}") do
Class.new do
if i % num == 0
define_method("#{i}") do
call
end
end
@JonathanWThom
JonathanWThom / helpers.scss
Last active March 27, 2019 22:35
Generating margin and padding helpers with SASS
$styles: margin padding;
$directions: left right top bottom;
@each $style in $styles {
@for $i from 0 through 50 {
.#{$style}-#{$i} { #{$style}: #{$i}px; }
@each $direction in $directions {
.#{$style}-#{$direction}-#{$i} { #{$style}-#{$direction}: #{$i}px }
}
}
@JonathanWThom
JonathanWThom / readyourself.rb
Last active September 18, 2018 05:30
Read current file, write current file's contents to current file, and execute again. Gets out of hand quickly.
# You'll probably want to Ctrl+C out of this one pretty quickly.
File.open($0, "r+") do |f|
content = f.read
puts content
f << content
f.close
sleep 5
system "ruby", $0
@JonathanWThom
JonathanWThom / add_timestamps_rails.rb
Last active January 6, 2018 22:27
Add missing timestamps to previously existing tables in Rails
## rails g migration add_timestamps
## Insert the following, with whatever version of ActiveRecord you're using.
class AddTimestamps < ActiveRecord::Migration[5.0]
def change
ActiveRecord::Base.connection.tables.each do |table_name|
columns = ActiveRecord::Base.connection.columns(table_name).map { |c| c.name }
["created_at", "updated_at"].each do |timestamp|
unless columns.include?(timestamp)
add_column table_name, timestamp, :datetime