Skip to content

Instantly share code, notes, and snippets.

@drbrain
drbrain / git-default
Last active August 18, 2023 18:45
Switch to the default branch of this git repository
#!/bin/sh
# git-default
#
# Switch to the repository's default branch
default=`git symbolic-ref refs/remotes/origin/HEAD | cut -d '/' -f 4`
current=`git branch --show-current`
if [ "x${default}" = "x${current}" ]; then
exit 0
@drbrain
drbrain / git-cleanup-repo
Last active December 3, 2023 08:47 — forked from robmiller/git-cleanup-repo
A script for cleaning up Git repositories. It deletes branches that are fully merged into the origin default branch, prunes obsolete remote tracking branches, and as an added bonus will replicate these changes on the remote.
#!/usr/bin/env nu
# Adapted from original by Yorick Sijsling
# Adapted from bash version by Rob Miller <rob@bigfish.co.uk>
#
# Nushell version
#
# See history for a less-capable bash version
#
# Compared to the fork point this version has additional features:
# * The default branch is automatically discovered (also in prior bash version)
@drbrain
drbrain / console
Last active October 19, 2020 00:29
Ruby frozen string interning
$ ruby not_frozen.rb
400600
# The string "a" is embedded inside the String object which is 40 bytes
#
# So 400600/40 = ~10,015 string objects
#
# (IIRC, up to 24 bytes of string may be embedded in the String)
$ ruby frozen.rb
$ ruby -v t.rb
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
Warming up --------------------------------------
if else 186.397k i/100ms
case with comparison 149.445k i/100ms
case all range 127.260k i/100ms
case hash cheat 190.074k i/100ms
Calculating -------------------------------------
if else 3.447M (± 7.5%) i/s - 17.149M in 5.007151s
case with comparison 2.388M (±18.2%) i/s - 11.657M in 5.065894s
@drbrain
drbrain / extract_certificates.rb
Created January 30, 2020 23:12
Ruby TLS tools
#!/usr/bin/env ruby
##
# Use this tool to extract X509 certificates from an HTTP server you want to
# connect to that is using a self-signed certificate. This is a
# Trust-on-First-Use operation so you are still at risk of various attacks
# including man-in-the-middle.
#
# This does allow you to uniformly use VERIFY_PEER across all HTTPS
# connections, as you now will be verifying the possibly-suspect connection
@drbrain
drbrain / Many runs
Last active May 17, 2019 01:03
benchmark/ips can mislead you
Warming up --------------------------------------
range 60.821k i/100ms
args 63.056k i/100ms
Calculating -------------------------------------
range 774.236k (± 7.5%) i/s - 3.893M in 5.057747s
args 797.590k (± 6.0%) i/s - 3.973M in 5.000220s
Warming up --------------------------------------
range 64.468k i/100ms
args 65.528k i/100ms
Calculating -------------------------------------
module A
module B
puts "module B inside module A nesting: #{Module.nesting}"
module_function
def show_nesting
puts "show_nesting nesting: #{Module.nesting}"
end
@drbrain
drbrain / Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡.rb
Created December 1, 2017 23:01
color Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡
def color(i)
color = 16 + (i * 13) % 216
"\e[38;5;#{color}m"
end
5.times { puts }
zalgo = "Z̤̯̤͈̲̠͋͟A̷̞͌̍̆̓L̺̙͖G̜̣̱̃̍́ͮO̤͙̫͔̔́̏͐̇͡!̷͙͉͖̋ͩ̓̇̿"
@drbrain
drbrain / ugh.rb
Last active November 9, 2017 00:57
You can't use `enum_for __method__` with inheritance 😢
class A
def m(&block)
#return enum_for __method__ unless block_given?
# The above calls the B implementation of #m so instead we have to create
# our own Enumerator that's bound to this class' method so we can run the
# with-block version of this method
unless block_given? then
enum = Enumerator.new do |yielder|
method = A.instance_method(__method__).bind self
@drbrain
drbrain / run
Created November 2, 2017 22:46
Don't convert hash keys, it's slower than using the "natural" key of the input data
$ ruby -v t.rb
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin17]
Lookup
Warming up --------------------------------------
Symbol Hash, Symbol Key
259.197k i/100ms
String Hash, String Key
231.953k i/100ms
Symbol Hash, String Key
203.959k i/100ms