Skip to content

Instantly share code, notes, and snippets.

@zdennis
zdennis / print-top-5-contributors-per-file.rb
Last active January 25, 2022 22:36
Git top contributors by file
#!/usr/bin/env ruby
#
# Prints out top 5 contributors per file (as determined by # of commits) on every file in the repository
#
files = `git ls-files`.lines.map(&:chomp)
stats_by_file = {}
files.each_with_index do |file, index|
@zdennis
zdennis / struct_that_disallows_nil_values.rb
Created March 31, 2021 21:20
Struct That Disallows nil
module StructThatDisallowsNil
def self.new(*args)
Struct.new(*args).tap do |klass|
klass.prepend Module.new {
def initialize(*)
super
members.each do |member|
fail "#{member} cannot be nil" if send(member).nil?
end
end
DisableSettingInverse.disabled = true
child = Child.new(parent_id: 12345)
parent = Parent.new
parent.children = [child]
child.parent_id # => 12345
DisableSettingInverse.disabled = false
@zdennis
zdennis / foo.sh
Created April 8, 2020 15:53
Bash: checking shell errexit shell option inside function, disabling it, and then restoring it.
#!/usr/bin/env bash
# Show how to check, save, restore the errexit shell option that is commonly set
# using "set -e". This is useful for functions that care about non-zero exit statuses
# and want to do something because of it such as a retry function.
set -eu
echo "Shell options: $-"
@zdennis
zdennis / rails-current
Created April 7, 2020 17:19
script/rails-current and script/rails-next dual booting command runners
#!/usr/bin/env ruby
require "optparse"
SCRIPT_NAME = File.basename(__FILE__)
ARGS_TO_FORWARD = ARGV.dup
ARGV.delete_if { |arg| arg !~ /-h|--help/ }
OptionParser.new do |opts|
@zdennis
zdennis / unmerged-commits
Created February 17, 2020 17:12
look for unmerged commits for a rails5/integration branch
#!/usr/bin/env bash
# A simple front-end for running git unmerged in a project-specific way.
#
# ## Options
#
# -l for local comparing against local branches. The default is to compare against remote branches.
#
INCLUDE_BRANCH_CONVENTION="rails5/"
@zdennis
zdennis / Gemfile
Last active February 13, 2020 15:46
bootboot-lite: dual boot without bootboot!
source "https://rubygems.org"
ruby "~> " + File.read("#{Bundler.root}/.ruby-version").strip
require_relative 'dual_boot/bootboot-lite'
if ENV['DEPENDENCIES_NEXT']
Bundler.settings.app_cache_path = "vendor/cache-rails_5.0"
# Add any gem you want here, they will be loaded only when running
@zdennis
zdennis / find-dependent-gems
Last active January 29, 2020 20:26
utility script to find which gems are dependent on a given gem provided a filter.
#!/usr/bin/env ruby
require 'optparse'
require 'shellwords'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"
opts.on("-g", "--gem=GEM") do |gem|
@zdennis
zdennis / load-manual-urls.rb
Last active December 20, 2019 18:31
Ruby script to manually load/close URLs in Safari
#!/usr/bin/env ruby
##
# ruby load-manual-urls.rb --urls manual-urls.txt
#
require 'optparse'
require 'csv'
STDOUT.sync = true
@zdennis
zdennis / kyle.rb
Created December 3, 2019 16:58
Treat all arguments a command to be run.
#!/usr/bin/env ruby
MAX_RETRIES = 4
SLEEP=2
COMMAND=ARGV.join(' ')
count = 0
loop do
puts "Running: #{COMMAND.inspect}"
unless system(COMMAND)