Skip to content

Instantly share code, notes, and snippets.

@zdennis
zdennis / notify-self.sh
Last active October 30, 2019 13:21
Send yourself notification center notifications from the command line...
#!/usr/bin/env bash
#
# Example:
#
# do_something && notify-self "Successfully did thing!" || notify-self "Failed doing thing!"
#
MESSAGE="$1"
TITLE="Mm-hmm..."
osascript -e "display notification \"$MESSAGE\" with title \"$TITLE\""
@zdennis
zdennis / database-stats.sql
Created October 25, 2019 18:11
PostgreSQL database table size statistics
WITH table_sizes1 AS (
SELECT
table_name,
table_size AS table_size,
indexes_size AS indexes_size,
total_size AS total_size
FROM (
SELECT
table_name,
pg_table_size(table_name) AS table_size,
@zdennis
zdennis / grab
Created September 18, 2019 21:26
shell script: grab
#!/usr/bin/env ruby
if STDIN.tty?
def print_help
puts <<-EOT.gsub(/^\s+\|/, '')
|Usage: ls | grab <column number>
| grab <column number> < INFILE
| grab <column number> <column number2> <column number3> ... < INFILE
|
|grab outputs a specific column of content from STDIN.
@zdennis
zdennis / find_duplicate_callback_declarations.rb
Created August 8, 2019 18:15
Rails 2 to Rails 3: find duplicate callback registrations
# When moving from Rails 2 to Rails 3 a change in
# callback behavior can cause issues because
# you can now only have one callback that calls a
# specific method, e.g.
#
# after_save :foo, if: :bar?
# after_save :foo, if: :baz?
#
# In the above only the second callback will be stored. The first
# one with the condition on bar? will not appear in the callback chain.
@zdennis
zdennis / mark_specs_as_rails_3.rb
Last active August 2, 2019 16:40
Rewriting spec files to add `rails3: true`
# requires pry, parser, and ast gems
#
# It also expects that you have build a file of spec files that should be modified. This file
# should contain one path/to/file per line.
#
# Running:
# cat list_of_passing_spec_files.txt | xargs ruby-rewrite --load 'mark_specs_as_rails_3.rb' -m
require 'pry'
@zdennis
zdennis / git_restoring_a_commit_you_amended_over.md
Created July 20, 2019 16:12
Git: Restoring a commit that you amended over

Here’s a quick walkthrough of my own process. I’ve often amended a commit like this:

shell> echo "bar" >> a
shell> git commit -a -m "made a bar"
shell echo "baz" >> a
shell> git commit --amend -C HEAD -a

Only to notice it was a whoops! I may have noticed it right away or noticed it after pushing, or noticed it the next day. So, I’ll run git reflog:

shell&gt; git reflog
@zdennis
zdennis / the_world_of_foos.rb
Last active July 16, 2019 16:11
Given the below hierarchy how many ways can you think of to wrap the `Parent#foo` implementation so that `Grandchild1` and `Grandchild2` can have their own unique logic that executes after all of the mixin `foo`(s) but before the `Parent#foo` executes?
module N
def foo
puts "N: foo"
super
end
end
module O
def foo
puts "O: foo"
@zdennis
zdennis / gitaliases
Last active June 28, 2019 18:25
Git aliases for opening sha URLs
# +url+ displays the URL for origin. Works with https:// and git://
url = !"git remote show origin | grep 'Fetch URL' | ruby -e \"puts 'https://' + STDIN.read.scan(/Fetch URL: git(?:@|:\\/\\/)(.*)/).join.sub('git://', '').sub(':', '/').sub('.git','')\""
# +open-url+ opens the home page for this repository
open-url = !"open `git url`"
# +sha+ displays the current SHA
sha = log -n1 --format="%H"
# +sha-url+ shows the current show url for github
@zdennis
zdennis / this_does_not_work.rb
Last active June 13, 2019 14:12
aliasing methods stack level too deep issue when a module is included more than once
module M
def self.included(base)
base.class_eval do
alias_method :user_without_name, :user
alias_method :user, :user_with_name
end
end
def user
puts "in user"
@zdennis
zdennis / gist:ea4bd6f53a1fcdf1200186eecf5f46ef
Created May 28, 2019 14:34
Run Ralis 3 specs individually
#!/usr/bin/env bash
>passed.txt
>failed.txt
for file in `find spec -name '*_spec.rb'` ; do
script/rails3 rspec --default-path spec_rails_3 $file
if [ $? -eq 0 ]; then
echo $file >> passed.txt
else