Skip to content

Instantly share code, notes, and snippets.

View JonathanWThom's full-sized avatar
🐶

Jonathan Thom JonathanWThom

🐶
View GitHub Profile
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>World's Most Annoying Phone Input</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
# Really this just figures out how many times [1, 2, 3].sample returns a given
# result vs the other two, which - shocker - is 1/3 (though that's kind of the
# point of the problem). But the output makes the result of _switching_ being
# the right decision a bit more intuitive to me.
class MontyHall
def initialize(num_games)
@num_games = num_games
@stand_pat_win_count = 0
@switch_win_count = 0
@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